vim入门使用与配置

来源:互联网 发布:卡盟官网源码带后台 编辑:程序博客网 时间:2024/06/07 02:12

vim相关知识积累,持续更新中。

常用操作


一般模式

  • n1,n2s/word1/word2/gn1行与n2行间寻找word1,替换为word2
  • 1,$s/word1/word2/g第一行到最后一行寻找word1,替换为word2
  • N[Enter] 向下移动n行
  • 0 : 移动到该行最后一个字符
  • $ : 移动到该行第一个字符
  • G : 最后一行
  • gg : 首行
  • x&X: x相当于[Del],X相当于[Backspace]
  • dd : 删除该行
  • yy : 复制该行
  • nyy: 向下复制n行
  • p&P: p贴到下一行,P贴到上一行
  • u : 复原前一个操作
  • / : 查找,可使用【n】键(next),在结果间跳转

切换到命令行

  • :!command 暂时离开执行command的显示结果
  • set nu 显示行号

块选择

按下v或者V或者[Ctrl]-v时,这个时候光标经过的地方会开始反白,y键可复制,d键可删除

替换文本中指定字符

在命令模式下

:%s/需要被替换的字符/新的字符/g

% 为正则表达式,表示文本结束,s 为 substitute 首字符,g 表示全局

环境设置与记录


vim的环境设置参数有很多,如果你想要知道目前的设置值,可以再一般模式时输入set all来查阅,不过设置选项实在太多了,在这里列出一些平时比较常用的一些简单的设置值

也可以通过配置文件来直观规定我们习惯的vim操作环境。整个vim的设置值一般是放置在/etc/vimrc这个文件中,不过,不建议直接修改它。建议修改文件~/.vimrc这个文件(默认不存在,请自行手动创建),将希望的设置值列入。

set hlsearch        "高亮度反白set backspace=2     "课随时用退格键删除set autoindent      "自动缩排set ruler           "课显示最后一行的状态set showmode        "左下角那一行的状态set nu              "行号set bg=dark         "显示不同的底色色调syntax on           "语法高亮

一份不错的 vim 环境配置文件

"basicimap yy <Esc>set nusyntax onset tabstop=4set softtabstop=4set shiftwidth=4set autoindentset cindentset cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1smap <C-a> "+yGmap <C-x> "+p"tab shortcutmap <A-1> 1gtmap <A-1> 2gtmap <A-1> 3gtmap <A-1> 4gtmap <A-1> 5gtmap <A-1> 6gtmap <A-1> 7gtmap <A-1> 8gtmap <A-1> 9gt"window shortcutmap <C-h> <C-w>hmap <C-j> <C-w>jmap <C-k> <C-w>kmap <C-l> <C-w>l"color schemeset t_Co=256colorscheme desertEx"encodingset encoding=utf-8set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,gbk,euc-jp,euc-kr,latin1if has("win32")    set fileencoding=chinese    " fix menu gibberish    source $VIMRUNTIME/delmenu.vim    source $VIMRUNTIME/menu.vim    " fix console gibberish    language messages zh_CN.utf-8else    set termencoding=utf-8    set fileencoding=utf-8endif"tagsmap <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>"/usr/include/tagsif has("win32")    set tags+=E:\workspace\linux\tags  " tags for /usr/include/else    set tags+=/usr/include/tags " tags for /usr/include/endifset tags+=tags                         " tags for current project"omnioppcopleteset nocpfiletype plugin onset completeopt=longest,menu      " I really HATE the preview window!!!let OmniCpp_NameSpaceSearch=1     " 0: namespaces disabled                                  " 1: search namespaces in the current buffer [default]                                  " 2: search namespaces in the current buffer and in included fileslet OmniCpp_GlobalScopeSearch=1   " 0: disabled 1:enabledlet OmniCpp_ShowAccess=1          " 1: show accesslet OmniCpp_ShowPrototypeInAbbr=1 " 1: display prototype in abbreviationlet OmniCpp_MayCompleteArrow=1    " autocomplete after ->let OmniCpp_MayCompleteDot=1      " autocomplete after .let OmniCpp_MayCompleteScope=1    " autocomplete after ::autocmd FileType python set omnifunc=pythoncomplete#Completeautocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJSautocmd FileType html set omnifunc=htmlcomplete#CompleteTagsautocmd FileType css set omnifunc=csscomplete#CompleteCSSautocmd FileType xml set omnifunc=xmlcomplete#CompleteTagsautocmd FileType php set omnifunc=phpcomplete#CompletePHPautocmd FileType c set omnifunc=ccomplete#Complete"compileif(has("win32") || has("win95") || has("win64") || has("win16"))    let g:iswindows=1else    let g:iswindows=0endif"单个文件编译map <F9> :call Do_OneFileMake()<CR>function Do_OneFileMake()    if expand("%:p:h")!=getcwd()        echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None        return    endif    let sourcefileename=expand("%:t")    if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))        echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None        return    endif    let deletedspacefilename=substitute(sourcefileename,' ','','g')    if strlen(deletedspacefilename)!=strlen(sourcefileename)        echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None        return    endif    if &filetype=="c"        if g:iswindows==1            set makeprg=gcc\ -o\ %<.exe\ %        else            set makeprg=gcc\ -o\ %<\ %        endif    elseif &filetype=="cpp"        if g:iswindows==1            set makeprg=g++\ -o\ %<.exe\ %        else            set makeprg=g++\ -o\ %<\ %        endif        "elseif &filetype=="cs"        "set makeprg=csc\ \/nologo\ \/out:%<.exe\ %    endif    if(g:iswindows==1)        let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'.exe','g')        let toexename=outfilename    else        let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'','g')        let toexename=outfilename    endif    if filereadable(outfilename)        if(g:iswindows==1)            let outdeletedsuccess=delete(getcwd()."\\".outfilename)        else            let outdeletedsuccess=delete("./".outfilename)        endif        if(outdeletedsuccess!=0)            set makeprg=make            echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None            return        endif    endif    execute "silent make"    set makeprg=make    execute "normal :"    if filereadable(outfilename)        if(g:iswindows==1)            execute "!".toexename        else            execute "!./".toexename        endif    endif    execute "copen"endfunction"进行make的设置map <F6> :call Do_make()<CR>map <c-F6> :silent make clean<CR>function Do_make()    set makeprg=make    execute "silent make"    execute "copen"endfunction"debugmap <F8> :call Do_OneFileDebug()<CR>function Do_OneFileDebug()    if expand("%:p:h")!=getcwd()        echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None        return    endif    let sourcefileename=expand("%:t")    if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))        echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None        return    endif    let deletedspacefilename=substitute(sourcefileename,' ','','g')    if strlen(deletedspacefilename)!=strlen(sourcefileename)        echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None        return    endif    if &filetype=="c"        if g:iswindows==1            set makeprg=gcc\ -g\ -o\ %<.exe\ %        else            set makeprg=gcc\ -g\ -o\ %<\ %        endif    elseif &filetype=="cpp"        if g:iswindows==1            set makeprg=g++\ -g\ -o\ %<.exe\ %        else            set makeprg=g++\ -g\ -o\ %<\ %        endif        "elseif &filetype=="cs"        "set makeprg=csc\ \/nologo\ \/out:%<.exe\ %    endif    if(g:iswindows==1)        let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'.exe','g')        let toexename=outfilename    else        let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'','g')        let toexename=outfilename    endif    if filereadable(outfilename)        if(g:iswindows==1)            let outdeletedsuccess=delete(getcwd()."\\".outfilename)        else            let outdeletedsuccess=delete("./".outfilename)        endif        if(outdeletedsuccess!=0)            set makeprg=make            echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None            return        endif    endif    execute "silent make"    set makeprg=make    execute "normal :"    if filereadable(outfilename)        if(g:iswindows==1)            execute "!gdb\ ".toexename        else            execute "!gdb\ ./".toexename        endif    endif    execute "copen"endfunction
0 0