[经验总结]我的VIM配置文件

本文详细介绍了一位用户的Vim配置文件(.vimrc),包括设置、插件启用、语法高亮等功能,同时还提供了多种自定义快捷键,如F2保存文件、F3切换窗口等,极大地提高了编辑效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"************************************************************************
"* filename: .vimrc
"* brief:    Happy VIMMING!  
"* creator:  thinkhy
"* date:     2008年 09月 10日 星期三 22:23:30 CST
"* modify:   2010年 01月 02日 星期六 20:00:59 CST
"************************************************************************
set fileencodings=ucs-bom,utf-8,chinese
set fileencoding=utf-8
set gfn=FreeMono/ 14
set awa " 自动保存
set ai " 自动缩进
set expandtab
set shiftwidth=4
set tabstop=4



filetype plugin on
syntax enable " 语法高亮
set ai " Auto indent
set si " Smart indent
set cindent " C缩进
set cino=g0 " public private friend 顶格写
set nocompatible " 不与VI兼容
set ruler " 显示光标位置
set nobackup " ~文件烦人
set ve=all " 逍遥游
"set iskeyword=@,48-57,192-255 "把默认keyword中的_去掉 还是用默认的吧
"不然Ctags不好使
set acd " autochangedir
set statusline=[%{strftime('%y-%m-%d/ %a')}][%{&ff}][%Y]%k[%{(&fenc==/"/")?&enc:&fenc}%{(&bomb?/",BOM/":/"/")}]%=[Line:%l/%L,Column:%c%V]/ %P
" ===========================================================================================
" Key map

" <C-q> 粘贴系统剪贴板的内容
nmap <C-q> "+p
imap <C-q> <ESC>"+p

" Enter 代替 ESC
inoremap <CR> <ESC>

" F2 保存文件
nmap <F2> :w<CR>
imap <F2> <ESC>:w<CR>

" F3 切换窗口
nmap <F3> <C-w>w
imap <F3> <ESC><C-w>w

" F4 关闭窗口
nmap <F4> <C-w>c
imap <F4> <ESC><C-w>c

" F5 Make
nmap <F5> :copen<CR>:make!<CR>
imap <F5> <ESC>:copen<CR>:make!<CR>


" F7 调整格式 用得太少 改作ptag
"nmap <F7> :%!indent -kr<CR>
"imap <F7> <ESC>:%!indent -kr<CR>

nmap <F7> <C-w>}<CR>
imap <F7> <ESC><C-w>}<CR>

" F8 打开taglist
nmap <F8> :TlistToggle<CR>
imap <F8> <ESC>:TlistToggle<CR>

" F9 编译链接C/C++文件
nmap <F9> :!g++ % -o %<<CR>
imap <F9> <ESC>:!g++ % -o %<<CR>


" F11 打开Winmanager
nmap <F11> :WManager<CR>
imap <F11> <ESC>:WManager<CR>

" F12 建立Tag文件
nmap <F12> :!ctags -R --c-kinds=+p --fields=+S<CR>
imap <F12> <ESC>:!ctags -R --c-kinds=+p --fields=+S<CR>

" <space> 空格
nmap <space> o<Esc>
 
" 调整窗口大小
map <Up> <Esc>:winc+<CR>
map <Down> <Esc>:winc-<CR>
map <Left> <Esc>:winc><CR>
map <Right> <Esc>:winc<<CR>

nmap F :BufExplorer<CR>
" 水木中收的技巧
" 设置多文件搜索快捷键,搜索完毕后不自动跳转
" 这个在GVIM与GUI的局快捷键冲突,在终端中与终端窗口的快捷键冲突
"nmap <M-f> *:vimgrep //<<c-r><c-w>/>/j **/*.[ch] **/*.txt **/*.cpp<cr>

" 顺序或倒序遍历quickfix中列出的item
nnoremap    <F9>          :cp<cr>
inoremap    <F9>          <c-o>:cp<cr>
nnoremap    <F10>         :cn<cr>
inoremap    <F10>         <c-o>:cn<cr>
"
" For C/C++ file

" Proposed by winmanager plugin
map <c-w><c-f> :FirstExplorerWindow<cr>
map <c-w><c-b> :BottomExplorerWindow<cr>
map <c-w><c-t> :WMToggle<cr>
"=========================================================================================
"技巧
"
" 水木中介绍解决输入法切换的问题
autocmd InsertEnter * set noimdisable
autocmd InsertLeave * set imdisable

au BufRead,BufNew /home/thinkhy/work/apue.2e/*  set tags+=/home/thinkhy/work/apue.2e/tags
au BufRead,BufNew /home/thinkhy/work/wine/*  set tags+=/home/thinkhy/work/wine/wine-1.1.35/tags

au BufRead,BufNew Makefile set noexpandtab
au BufRead,BufNew makefile set noexpandtab

" 这个技巧看上去很好,但和我的习惯不符,还是^O和Tab搭配着好用。
" smth VIM版flw 介绍的 Tab用法
" if version >= 700
" nmap <silent> <TAB> <C-W>w:set cursorline<CR>:200sl m<CR>:set nocursorline<CR>
" nmap <silent> <S-TAB> <C-W>p:set cursorline<CR>:200sl m<CR>:set nocursorline<CR>
" else
" nmap <silent> <TAB> <C-W>w
" nmap <silent> <S-TAB> <C-W>p
" endif

"autocmd CmdWinEnter * inoremap <buffer> <Esc> <C-o>:exit<CR><Esc> /| /
"set noimdisable|set insertmode
"autocmd CmdWinLeave * set imdisable | set noinsertmode
"cnoremap <C-space> <C-f>

"imap <Tab> <Space><Space><Space><Space><Space><Space><Space><Space

"===========================================================
" For xml file
"===========================================================
"so ~/vimfiles/plugin/xml.vi
autocmd FileType xml,xsd,xsl :set tabstop=4 expandtab

let g:ChineseIMESpaceToggle=1
"let g:ChineseIMEMappingCtrl6=1
setlocal pumheight=1
setlocal completeopt=menu,preview,longest
"nmap <F12> :w&&%!xmllint % --format<CR

"imap <F12> <ESC>:%!xmllint % --format<CR

" for C++ tags  [09/11/30 thinkhy]
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR> 


">=================================================
">=================================================
" [ Added by thinkhy 2010年 01月 02日 星期六 19:54:24 CST]
" 水木VIM版  15322贴
"发信人: flw (梨花小蓓蕾), 信区: VIM
"标  题: 分享几个我自认为比较得意的自定义快捷键
"发信站: 水木社区 (Mon Dec 28 10:34:46 2009), 站内

" 换标签页
nmap <C-N>  :tabnext<CR>
nmap <C-P>  :tabprev<CR>

" 换窗口
nmap <silent><TAB>  <c-w>w:set cursorline<CR>:100sl m<CR>:set nocursorline<CR>
nmap <silent><S-TAB> <c-w>p:set cursorline<CR>:100sl m<CR>:set nocursorline<CR>

function! HelpHelper( where )
    " 先拿下光标下的文字
    
let l:word = expand( "<cWORD>" )

    " 再取出疑似函数名/模块名的部分和疑似方法的部分
    " /([_a-zA-Z0-9:]/+/)/(->/([_a-zA-Z0-9]/+/)/)/?
    
let l:lists = matchlist( l:word, "//([_a-zA-Z0-9:]//+//)//(->//([_a-zA-Z0-9]//+//)//)//?" )

    
let l:name = l:lists[1]
    
let l:method = l:lists[3]

    
let l:progList = []

    
if &ft == "perl"
        
if match( l:name, "::" ) != -1
            " 函数里是不可能有 :: 的,不用想了,一定是模块
            
let l:progList += [ [ "perldoc -u ", "pod" ] ]
        
elseif strlen( l:method )
            " 有方法的,一定是模块了
            
let l:progList += [ [ "perldoc -u ", "pod" ] ]
        
elseif match( l:name, "[A-Z]" ) != -1
            " 有大写字母,那一定是模块了,Perl buildin 函数的名称全都是小写字母的
            
let l:progList += [ [ "perldoc -u ", "pod" ] ]
        
else
            " 剩下的,有可能是函数,也有可能是模块,
            " 但是考虑到这样的模块较少,因此先优先查函数
            
let l:progList += [ [ "perldoc -u -f ", "pod" ], [ "perldoc -u ", "pod" ] ]
        
endif
    
endif

    
if &ft == "python"
        
let l:progList += [ [ "python -c /"import sys; a = sys.argv[-1]; b = a.find('.') > -1 and 1 or 0; m = b == 1 and '.'.join(a.split('.')[:-1]) or a; exec( 'import '+m+';help('+a+')' )/" ", "man" ] ]
    
endif

    
if &ft == "erlang"
        
if match( l:name, ":" ) != -1
            " erlang 的模块方法
            
let l:lists = matchlist( l:name, "//([_a-zA-Z0-9:]//+//)://([_a-zA-Z0-9]//+//)" )
            
let l:name = l:lists[1]
            
let l:method = l:lists[2]
        
endif
        
let l:progList += [ [ "PAGER=/"col -b/" erl -man ", "man" ] ]
    
endif

    
let l:progList += [ [ "PAGER=/"col -b/" man 3 ", "man" ] ]
    
let l:progList += [ [ "PAGER=/"col -b/" man 2 ", "man" ] ]
    
let l:progList += [ [ "PAGER=/"col -b/" man ", "man" ] ]

    
let l:found = 0
    " 下面调用外部命令,取帮助信息

    " [Added by thinkhy 10/01/02]
    
if &ft == "vim"
        
echomsg "I am vim"
        
try
            
execute " h " . l:word
        
catch /foo/
        
endtry
        
execute "wincmd p"
        
return
    
endif
    " [End thinkhy]

    
for l:prog in l:progList
        
let l:progName = l:prog[0]
        
let l:fileType = l:prog[1]
        
let l:cmd = l:progName . l:name
        
let l:lines = system( l:cmd )
        
if !v:shell_error
            
if a:where ==? "inNewTab"
                
tabe
            
elseif a:where ==? "Vertical"
                
rightbelow new
            
else
                
above new
            
endif

            
set buftype = nofile
            
set ts = 8
            
execute "set ft=" . l:fileType
            
call append( 0, split( l:lines, "/n" ) )
            
normal gg
            
if strlen(l:method) " 跳转到方法
                
if l:fileType == "pod"
                    " regex: /(=item/d*/s/+/)/@<= />
                    
call search( "//(=item//d*//s//+//)//@<=" . l:method . "//>" )
                
elseif l:fileType == "man"
                    " regex: /(^/s/+/)/@<=l:method/>  
                    
call search( "//(^//s//+//)//@<=" . l:method . "//>" )
                
endif
            
endif

            
let l:found = 1
            
break
        
endif
    
endfor

    
if !l:found
        
echohl ErrorMsg | echomsg "^_^ 没有帮助信息。" | echohl None
    
endif

endfunction

" vim 内打开 man/perldoc/erl -man/python help
autocmd FileType c,perl,erlang,python,vim nmap <silent> K :call HelpHelper( "" )<CR>
autocmd FileType c,perl,erlang,python,vim nmap <silent> gK :call HelpHelper( "InNewTab" )<CR>

">==========================================================================
"发信人: Dieken (风催草低 - 明月何尝不照人), 信区: VIM
"标  题: Re: 分享几个我自认为比较得意的自定义快捷键
"发信站: 水木社区 (Mon Dec 28 12:00:38 2009), 站内
" 我也来:

" Press v in quickfix window to preview
" 虽然没看懂zz:的意思,好使
au FileType qf :nnoremap <buffer> v <Enter>zz:wincmd p<Enter>

" for gf command to open java source file at import clause
au FileType java :set suffixesadd+=.java

"
" nnoremap <c-m> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR><CR>

" 将搜索结果放在copen窗口
" ----------------------------------------------------------------------------
" * :lop[en] [height]   打开一个窗口显示当前窗口的位置列表。只有当前窗口有位置
" *       列表的情况才能用。你可以同时打开多于一个的位置列表窗
" *       口。除此以外,和 ":copen" 相同。
" ----------------------------------------------------------------------------
nnoremap <c-m> :g//laddexpr expand("%") . ":" . line(".") . ":" .  getline(".")<CR>:lopen<CR><CR>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值