我的轻度定制vim
vim的学习曲线又陡又长,但它的功能和可定制性实在太强大,一个熟练的用户加上一个高度定制化的vim能够达到很高的效率,在加上熟练地运用vim的各种高级功能是很酷的一件事,于是我决定近期内不再尝试其它的编辑器,逐步地打造适合自己的vim。下面记录的,都是我自己觉得很有用的,或者是容易忘记的,这篇文章的内容也会是逐步丰富的。
技巧们:
gd :跳到变量声明的地方
<Ctrl> + ] :跳到定义的地方,需要ctags事先生成tag文件
<Ctrl> + o :返回之前的位置
5 + <Ctrl> + ^ :跳到第5号buffer
<Ctrl> + PgUp/PgDn :在tab间跳
:ls 列出buffer
<Ctrl> + g 显示当前编辑文件中当前光标所在行位置以及文件状态信息
:r FILENAME 向当前文件中插入另外的文件的内容
J 把两行连起来
f/F 单字符查找命令,最有用的移动命令之一,"fx" 命令向前查找本行中的字符 x。"F" 命令则用于向左查找。
"tx" 命令与 "fx" 相似,但它只把光标移动到目标字符的前一个字符上。提示:"t" 表示 "To"。这个命令的反向版本是 "Tx"。
这四个命令可以通过 ";" 命令重复,"," 命令则用于反向重复。无论用哪个命令,光标永远都不会移出当前行,哪怕是这两行是连续的一个句子。
H,M,L:分别代表移到当前视野的Home, Middle, Last处
:qall 全部退出
:wqall 全部保存退出
vim中的替换:
% 表示全文匹配
:s/old/new/g 当前行中所有old替换成new;
:%s/old/new/ 表示将全文中old替换成new,但每行只替换第一个单词;
:%s/old/new/g 表示将全文中所有出现过的old替换成new (所有都替换); %s/old/new/gc 全文替换, 替换前询问; d 删除 g/china/d;
我的vimrc如下:
" 轻度定制的VIM" JH Gao <gaopenghigh@gmail.com>"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => 全局设置""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Turn backup offset nobackupset nowbset noswapfileset autoread " Set to auto read when a file is changed from the outsideset hid "Change buffer - without saving" map leader键设置" With a map leader it's possible to do extra key combinations" like <leader>w saves the current filelet mapleader = ","let g:mapleader = ","set showcmd " Show (partial) command in status line.set showmatch " Show matching brackets."set ignorecase " Do case insensitive matchingset linebreak " 整词换行set smartcase " Do smart case matchingset incsearch " 输入字符串就显示匹配点set hlsearch " high light search resultsset autowrite " 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。"set hidden " Hide buffers when they are abandoned"set mouse=a " Enable mouse usage (all modes)set nu"--状态行设置--set title " show title in console title barset laststatus=2 " 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行set ruler " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。" 编码设置set fenc=utf-8set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936" Vim jump to the last position when reopening a fileif has("autocmd") au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endifendif" Uncomment the following to have Vim load indentation rules and plugins" according to the detected filetype.if has("autocmd") filetype plugin indent onendif""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => UI设置""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""runtime! debian.vimsyntax onset background=darkcolorscheme desert "设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim73/colors目录""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => 格式设置tabs and indent""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""set tabstop=4set shiftwidth=4set smarttabset expandtab "输入:re可以把tab替换为空格set autoindentset ai "Auto indentset si "Smart indetset wrap "Wrap lines" 删除末尾的空格,对python等很有用func! DeleteTrailingWS() exe "normal mz" %s/\s\+$//ge exe "normal `z"endfuncautocmd BufWrite *.py,*.t2t,*.sh :call DeleteTrailingWS()"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => 在tabs和windows之间移动"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 简化在windows之间移动的操作map <C-j> <C-W>jmap <C-k> <C-W>kmap <C-h> <C-W>hmap <C-l> <C-W>l" Tab设置, <leader> 已经被设为','map <leader>tn :tabnew<cr>map <leader>te :tabeditmap <leader>tc :tabclose<cr>map <leader>tm :tabmove" 按<F2>在新tab中编辑文件, 注意下一行末尾是有个空格的:)nnoremap <F2> :tabedit" 按<F7>把所有buffer变成tab显示出来let notabs = 1nnoremap <silent> <F7> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>" 按<F8>和<Shift+F8>移动到下一个/上一个tabset switchbuf=usetabnnoremap <F8> :sbnext<CR>nnoremap <S-F8> :sbprevious<CR>" 按 ,1 ,2 ,3等跳到相应的tabmap <leader>1 1gtmap <leader>2 2gtmap <leader>3 3gtmap <leader>4 4gtmap <leader>5 5gtmap <leader>6 6gtmap <leader>7 7gtmap <leader>8 8gtmap <leader>9 9gt" 暂时用netrw来作为文件浏览器" 按 ,fe 在左侧打开文件浏览器" 打开后在文件上按 t 在新tab中打开这个文件let g:netrw_winsize = 30nmap <leader>fe :Sexplore!<cr>"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" => Cope, 还不太理解怎么用这个东东"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Do :help cope if you are unsure what cope is. It's super useful!map <leader>cc :botright cope<cr>map <leader>n :cn<cr>map <leader>p :cp<cr>""""""""""""""""""""""""""""""" => Python 的设置""""""""""""""""""""""""""""""let python_highlight_all = 1au FileType python syn keyword pythonDecorator True None False self""""""""""""""""""""""""""""""" => 插件设置""""""""""""""""""""""""""""""" txt2tagsau BufNewFile,BufRead *.t2t set ft=txt2tags" ctags 和 taglist"" 按下F8重新生成tag文件,并更新taglistmap <F8> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>imap <F8> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>set tags=tagsset tags+=./tags "add current directory's generated tags filelet Tlist_Ctags_Cmd = '/usr/bin/ctags'let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最后一个窗口,则退出vimlet Tlist_Show_One_File=0 "让taglist可以同时展示多个文件的函数列表let Tlist_File_Fold_Auto_Close=1 "非当前文件,函数列表折叠隐藏let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist窗口let Tlist_Process_File_Always = 1 "aglist始终解析文件中的tag,不管taglist窗口有没有打开" 用 F9 来打开/关闭taglist页面map <silent> <F9> :TlistToggle<cr>" lookupfile插件设置, 按<F5>搜索文件let g:LookupFile_MinPatLength = 2 "最少输入2个字符才开始查找let g:LookupFile_PreserveLastPattern = 0 "不保存上次查找的字符串let g:LookupFile_PreservePatternHistory = 1 "保存查找历史let g:LookupFile_AlwaysAcceptFirst = 1 "回车打开第一个匹配项目let g:LookupFile_AllowNewFiles = 0 "不允许创建不存在的文件if filereadable("./filenametags") "设置tag文件的名字let g:LookupFile_TagExpr = '"./filenametags"'endif"默认LUTags的快捷键为<F5>"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 上面的taglist和lookupfile都需要实时更新的tag文件,我定时运行下面的脚本实现这个目的" #!/bin/sh" # generate tag file for lookupfile plugin" dir=$1" echo -e "!_TAG_FILE_SORTED\t2\t/2=foldcase/" > $dir/filenametags" find $dir/ ! -wholename '*.svn*' -not -regex '.*\.\(png\|gif\|jpg\|pyc\)'" -type f -printf "%f\t%p\t1\n" | sort -f >> $dir/filenametags"" /usr/bin/ctags -R $dir""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" =>自动运行这个文件(python, bash)""""""""""""""""""""""""""""""" 写python或shell时经常需要做单元测试, 设置按下<F12>或<F10>就用python运行这个文件map <F12> :!python %<cr>map <F10> :!bash %<cr>"""""""""""""""""""""""""""""""""""""""""""""""""""" =>自动生成一些有用的信息"""""""""""""""""""""""""""""""""""""""""""""""""""autocmd BufNewFile *.sh,*.pl,*.py exec ":call SetTitle()""autocmd BufWrite *.sh,*.pl,*.py exec ":call ModifyTitle()"autocmd BufWrite *.sh,*pl,*py if getline(6) != "# Modify Author: ".expand("gaopenghigh@gmail.com") || split(getline(7))[3] != strftime("%F") | call ModifyTitle() | endiffunc SetTitle() if &filetype == 'sh' call setline(1, "\#!/bin/sh") call append(line("."), "\#****************************************************************#") call append(line(".")+1, "\# ScriptName: ".expand("%") ) call append(line(".")+2, "\# Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+3, "\# Create Date: ".strftime("%F %R")) call append(line(".")+4, "\# Modify Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+5, "\# Modify Date: ".strftime("%F %R")) call append(line(".")+6, "\# Function: " ) call append(line(".")+7, "\#***************************************************************#") call append(line(".")+8, "") :8 elseif &filetype == 'perl' call setline(1, "\#!/usr/bin/perl") call append(line("."), "\#****************************************************************#") call append(line(".")+1, "\# ScriptName: ".expand("%") ) call append(line(".")+2, "\# Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+3, "\# Create Date: ".strftime("%F %R")) call append(line(".")+4, "\# Modify Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+5, "\# Modify Date: ".strftime("%F %R")) call append(line(".")+6, "\# Function: ") call append(line(".")+7, "\#***************************************************************#") call append(line(".")+8, "") :8 elseif &filetype == 'python' call setline(1, "\#!/usr/bin/python") call append(line("."), "\# -*- coding: utf-8 -*-") call append(line(".")+7, "\#***************************************************************#") call append(line(".")+1, "\# ScriptName: ".expand("%") ) call append(line(".")+2, "\# Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+3, "\# Create Date: ".strftime("%F %R")) call append(line(".")+4, "\# Modify Author: ".expand("gaopenghigh@gmail.com") ) call append(line(".")+5, "\# Modify Date: ".strftime("%F %R")) call append(line(".")+6, "\# Function: ") call append(line(".")+7, "\#***************************************************************#") call append(line(".")+8, "") :8 endifendfuncfunc ModifyTitle() if getline(6) =~ "# Modify Author:.*" call setline(6, "\# Modify Author: ".expand("gaopenghigh@gmail.com") ) call setline(7, "\# Modify Date: ".strftime("%F %R")) endifendfunc 1 楼 niwtsew 2012-02-28 nice!add several more which I'm using
command Q q!
command Qa qa!
"and some cscope related
cs add $HOME/temp/jdk1.5.0_15/cscopejdk1.5.0_15.out