vim入门使用与配置

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 环境配置文件

"basic
imap yy <Esc>
set nu
syntax on
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set cindent
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
map <C-a> "+yG
map <C-x> "+p

"tab shortcut
map <A-1> 1gt
map <A-1> 2gt
map <A-1> 3gt
map <A-1> 4gt
map <A-1> 5gt
map <A-1> 6gt
map <A-1> 7gt
map <A-1> 8gt
map <A-1> 9gt

"window shortcut
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

"color scheme
set t_Co=256
colorscheme desertEx

"encoding
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,gbk,euc-jp,euc-kr,latin1
if has("win32")
    set fileencoding=chinese
    " fix menu gibberish
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    " fix console gibberish
    language messages zh_CN.utf-8
else
    set termencoding=utf-8
    set fileencoding=utf-8
endif

"tags
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
"/usr/include/tags
if has("win32")
    set tags+=E:\workspace\linux\tags  " tags for /usr/include/
else
    set tags+=/usr/include/tags " tags for /usr/include/
endif
set tags+=tags                         " tags for current project

"omnioppcoplete
set nocp
filetype plugin on
set 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 files

let OmniCpp_GlobalScopeSearch=1   " 0: disabled 1:enabled
let OmniCpp_ShowAccess=1          " 1: show access
let OmniCpp_ShowPrototypeInAbbr=1 " 1: display prototype in abbreviation
let OmniCpp_MayCompleteArrow=1    " autocomplete after ->
let OmniCpp_MayCompleteDot=1      " autocomplete after .
let OmniCpp_MayCompleteScope=1    " autocomplete after ::

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

"compile
if(has("win32") || has("win95") || has("win64") || has("win16"))
    let g:iswindows=1
else
    let g:iswindows=0
endif
"单个文件编译
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


"debug
map <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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。