vim入門使用與配置

來源:互聯網
上載者:User

標籤:配置   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

% 為Regex,表示文本結束,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

vim入門使用與配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.