:h %<
%< current file name without extension
我这里的配置是针对vim所设置如果是最原始的vi,那么是不支持下面一些配置的,如:
set mouse=a "enable mouseset autochdir
出错提示:E538: No mouse support: mouse=a
E518: Unknown option: autochdir
执行yum install vim后,给vim设置别名vi
[workhard@zl 毕业设计]$ which vi
/bin/vi
[workhard@zl 毕业设计]$ alias vi='vim'
[workhard@zl 毕业设计]$ which vi
alias vi='vim'
/usr/bin/vim
set nocompatible
"source $VIMRUNTIME/vimrc_example.vim
"source $VIMRUNTIME/mswin.vim
"behave mswin
"设置vim默认窗口大小
set lines=31
set columns=102
set mouse=a "enable mouse
syntax on
filetyp on
filetype plugin indent on
"autocmd FileType c set tabstop=8 "When edit c source code
"au BufNewFile,BufRead *.txt set tabstop=8 ""When edit TXT file
"新建.c,.h,.sh,.java文件,自动插入文件头
autocmd BufNewFile *.[ch],*.sh,*.cpp,*.java exec ":call SetTitle()"
"新建文件后,:h normal...Execute Normal mode commands {commands}. 跳到第12行
autocmd BufNewFile * normal 12G
set expandtab "Converting tabs to spaces
set tabstop=4
set shiftwidth=4
set autoindent
set cindent
set cino=g0
set nu
set diffexpr=MyDiff()
set tags=tags
set autochdir
".C/C++的编译和运行 Build and run
map <F9> :call CompileRunGcc()
func! CompileRunGcc()
call CompileGcc()
exec "!./%<"
endfunc
".C/C++的编译运行_end
".C/C++的编译
map <C-F9> :call CompileGcc()
function CompileGcc()
exec "w"
if &filetype=="c"
" %当前文件名 current file name
" %< 当前文件名(去掉后缀) current file name without extension
exec "!gcc -g % -o %<"
elseif &filetype=="cpp"
exec "!g++ -g % -o %<"
endif
endfunction
".C/C++的编译_end
".C/C++的运行
map <C-F10> :exec "!./%<"
".C/C++的运行_end
"Ctrl-F12快捷键自动生成tags文件
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
"Ctrl-F12快捷键自动生成tags文件_end
"Ctrl-F11快捷键自动生成单前文件的tags文件
map <C-F11> :!ctags %
"Ctrl-F11快捷键自动生成单前文件的tags文件_end
"vim自动更新标签文件tags
au BufWritePost *.cpp,*.h,*.c,*.rl,*.def call system("ctags --tag-relative -a -o ~/.vim/tags/usr/ctags/tags --extra=+q " . expand("%:p"))
set nobackup
let &termencoding=&encoding
set fileencodings=utf-8,gbk,cp936###
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
"定义函数SetTitle,自动插入文件头
func SetTitle()
call setline(1, "/**********************************************************************")
call append(line("."), "* Compiler: gcc 4.5.1 20100924 (Red Hat 4.5.1-4)")
call append(line(".")+1, "* Last Update: ".strftime("%c"))
call append(line(".")+2, "* File Name: ".expand("%"))
call append(line(".")+3, "* Arguments: ")
call append(line(".")+4, "* Description: ")
call append(line(".")+5, "************************************************************************/")
if &filetype == 'c'
call append(line(".")+6, "#include <stdio.h>")
call append(line(".")+7, "#include <stdlib.h>")
call append(line(".")+8, " ")
call append(line(".")+9, "int main(int argc, char **argv)")
call append(line(".")+10, "{")
call append(line(".")+11, " ")
call append(line(".")+12, "")
call append(line(".")+13, " return 0;")
call append(line(".")+14, "}")
elseif &filetype == 'cpp'
call append(line(".")+5, "#include <iostream>")
call append(line(".")+6, " ")
call append(line(".")+7, "int main()")
call append(line(".")+8, "{")
call append(line(".")+9, "")
call append(line(".")+10, " return 0;")
call append(line(".")+11, "}")
elseif &filetype == 'java'
call append(line(".")+5, "public class ".expand("%<").expand("{"))
call append(line(".")+6, " public static void main(String[] args){")
call append(line(".")+7, " ")
call append(line(".")+8, " System.out.println();")
call append(line(".")+9, " }")
call append(line(".")+10, "}")
call append(line(".")+11, " ")
endif
endfunc
" 自动更新文件的最后更新时间
function! AutoUpdateTheLastUpdateInfo()
let s:original_pos = getpos(".")
let s:regexp = "^\* Last Update: "
let s:lu = search(s:regexp)
if s:lu != 0
let s:update_str = matchstr(getline(s:lu), s:regexp)
call setline(s:lu, s:update_str . strftime(" %c"))
call setpos(".", s:original_pos)
endif
endfunction
autocmd InsertLeave *.{py,c,js,css},*vimrc call AutoUpdateTheLastUpdateInfo()
本文介绍了一个详细的Vim配置方案,包括设置默认窗口大小、鼠标支持、语法高亮等功能,并提供了针对C/C++代码的自动文件头插入、编译运行及标签文件生成等实用功能。
952

被折叠的 条评论
为什么被折叠?



