歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Vim的基本配置

Vim的基本配置

日期:2017/3/3 16:22:48   编辑:關於Linux

基本配置

終端運行:

vim ~/.vimrc

配置內容如下:

set nocp    "設置兼容  
set expandtab   "設置tab  
set shiftwidth=4    "設置tab的間隔  
set tabstop=4   "四個空格代表一個tab  
set sts=4  
set showmatch   "在輸入括號時光標會短暫地跳到與之相匹配的括號處  
set autoindent  "設置自動縮進  
set number  "設置是否顯示行  
set guifont=Monospace\ 12  "設置字體大小   
set encoding=utf-8  "設置編碼為utf-8  
set fileencoding=utf-8  
set fileencodings=ucs-bom,utf-8,GB18030,cp936,big5,euc-jp,euc-kr,latin1  
      
"自動補全配置  
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   
      
"#######中括號 大括號 小括號 自動補全  
:inoremap ( ()<ESC>i  
:inoremap ) <c-r>=ClosePair(')')<CR>  
:inoremap { {}<ESC>i  
:inoremap } <c-r>=ClosePair('}')<CR>  
:inoremap [ []<ESC>i  
:inoremap ] <c-r>=ClosePair(']')<CR>  
:inoremap < <><ESC>i  
:inoremap > <c-r>=ClosePair('>')<CR>  
      
function ClosePair(char)  
    if getline('.')[col('.') - 1] == a:char  
        return "\<Right>"  
    else<pre name="code" class="plain">sudo apt-get install exuberant-ctags  
</pre><br>        return a:char    endif endfunction"#######中括號 大括號 小括號 自動補全

代碼自動補全

首先安裝ctags

sudo apt-get install exuberant-ctags

在主目錄中創建.vim文件夾,然後按創建幾個子目錄:

cd .vim  
mkdir plugin  
mkdir doc  
mkdir tags

下載omnicppcomplete,點我下載

解壓後生產三個文件夾,after, autoload, doc我直接把這三個文件夾放在 .vim這個文件夾下。

還需要下載一個文件,用於生成cpp的索引-libstdc++ 頭文件

解壓後終端cd到解壓後的目錄,用ctags生產索引。

ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp .

目錄下面會多出一個cpp文件,這就是生成的索引文件。

將cpp文件拷貝進~.vim/tags文件夾中。

最後修改一下~/.vimrc.在最後添加

"-- omnicppcomplete setting --  
set tags+=/home/tao/.vim/tags/cpp  
set completeopt=menu,menuone  
filetype plugin indent on  
let OmniCpp_MayCompleteDot = 1 " autocomplete with .  
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->  
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::  
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)  
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files  
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype  in popup window  
let OmniCpp_GlobalScopeSearch=1  
let OmniCpp_DisplayMode=1  
let OmniCpp_DefaultNamespaces=["std"]

重啟vim,測試一下:

文件浏覽器

在進行多文件編輯的時候,如果沒有文件浏覽功能,不停的切換文件將會非常之蛋疼..

這裡要用到的插件是nerdtree,點我下載。

解壓後將plugin下的.vim和doc下的.txt文件拷貝到~/.vim下的對應目錄。

#命令行  
cp plugin/NERD_tree.vim ~/.vim/plugin/  
cp doc/NERD_tree.txt ~/.vim/doc

打開vim,執行命令:

:NERDTre

關於插件的使用:

進入當前目錄的樹形界面,通過小鍵盤上下鍵,能移動選中的目錄或文件

目錄前面有+號,摁Enter會展開目錄,文件前面是-號,摁Enter會在右側窗口展現該文件的內容,並光標的焦點focus右側。

ctr+w+h 光標focus左側樹形目錄,ctrl+w+l 光標focus右側文件顯示窗口。多次摁 ctrl+w,光標自動在左右側窗口切換

光標focus左側樹形窗口,摁? 彈出NERDTree的幫助,再次摁?關閉幫助顯示

輸入:q回車,關閉光標所在窗口

Copyright © Linux教程網 All Rights Reserved