基礎(chǔ)配置vim的配置是在用戶主目錄下的 ~/.vimrc 文件中完成的,如果沒有的話,需要自己新建一下: 首先做些簡單的配置: 1 2 3 4 5 6 7 8 9 10 | set nocompatible '關(guān)閉與 vi 的兼容模式
set number '顯示行號
set nowrap '不自動折行
set showmatch '顯示匹配的括號
set scrolloff=3 '距離頂部和底部3行'
set encoding=utf-8 '編碼
set fenc=utf-8 '編碼
set mouse=a '啟用鼠標(biāo)
set hlsearch '搜索高亮
syntax on '語法高亮
|
為py文件添加下支持pep8風(fēng)格的配置: 1 2 3 4 5 6 7 8 | au BufNewFile,BufRead *.py
\ set tabstop=4 'tab寬度
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79 '行最大寬度
\ set expandtab 'tab替換為空格鍵
\ set autoindent '自動縮進(jìn)
\ set fileformat=unix '保存文件格式
|
分割窗口vim在編輯的時候就可以打開多個文件: :vs 或者 :vsplit 將當(dāng)前窗口豎直分割,并在上面新窗口中顯示當(dāng)前文件 :vs filename 將當(dāng)前窗口豎直分割,新文件在新窗口中顯示 :sp 或者:sv或者:split 將當(dāng)前窗口水平分割,并在左邊新窗口中顯示當(dāng)前文件 :sp filename 將當(dāng)前窗口豎直分割,新文件在左邊新窗口中顯示 :new 新建文件并豎直分割 :vnew 新建文件并水平分割 如果想讓新窗口在右邊或者下方打開,添加配置: 1 2 | set splitbelow
set splitright
|
在窗口之間切換可以用鼠標(biāo),如果不想用鼠標(biāo),切換按鍵如下: Ctrl-w-j 切換到下方的分割窗口Ctrl-w-k 切換到上方的分割窗口Ctrl-w-l 切換到右側(cè)的分割窗口Ctrl-w-h 切換到左側(cè)的分割窗口
覺得三個按鍵多的話可以設(shè)置快捷鍵: 1 2 3 4 | nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
|
這樣就不用按w鍵了。 代碼折疊當(dāng)代碼行數(shù)很多的時候,代碼折疊是很必須的: 1 2 | set foldmethod=indent
set foldlevel=99
|
使用zc按鍵來創(chuàng)建折疊,使用za來打開或者關(guān)閉折疊。 za經(jīng)常會誤輸入,可以用空格鍵來替代za: 一鍵執(zhí)行python代碼如果想直接在vim中執(zhí)行python代碼,可以添加(來自https://www.zhihu.com/question/20271508): 1 2 3 4 5 6 7 | map <F5> :call RunPython()<CR>
func! RunPython()
exec 'W'
if &filetype == 'python'
exec '!time python2.7 %'
endif
endfunc
|
這樣,按F5鍵python代碼就可以自動執(zhí)行了 插件vim插件中最主要的就是vundle了,vundle用來管理vim的其它插件 VundleVundle 是 Vim bundle 的簡稱,使用git來管理vim插件,有了它,安裝其它插件就方便很多。 項目地址https://github.com/VundleVim/Vundle.vim。 首先下載源碼: 1 | git clone https: / / github.com / VundleVim / Vundle.vim.git ~ / .vim / bundle / Vundle.vim
|
如果~/.vim/bundle目錄不存在,則新建目錄: 1 2 3 4 | cd ~
mkdir .vim
cd .vim
mkdir bundle
|
然后將下列配置放在.vimrc文件的開頭: 1 2 3 4 5 6 7 8 9 10 11 12 13 | set nocompatible ' be iMproved, required
filetype off ' required
' set the runtime path to include Vundle and initialize
set rtp + = ~ / .vim / bundle / Vundle.vim
call vundle #begin()
' let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
' All of your Plugins must be added before the following line
call vundle #end() ' required
filetype plugin indent on ' required
|
如果想下載某個插件,比如自動縮進(jìn)indentpython.vim插件,需要將 1 | Plugin 'vim-scripts/indentpython.vim'
|
置于call vundle#begin()和call vundle#end()之間,保存配置后在vim中執(zhí)行 即可以自動下載indentpython.vim插件了。 bundle可以管理下載幾種不同的插件,方式如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 | github上的插件
Plugin 'tpope/vim-fugitive'
來自于http: / / vim - scripts.org / vim / scripts.html的插件
Plugin 'L9'
非github上的git插件
Plugin 'git://git.wincent.com/command-t.git'
本地插件
Plugin 'file:///home/gmarik/path/to/plugin'
' The sparkup vim script is in a subdirectory of this repo called vim.
' Pass the path to set the runtimepath properly.
' Plugin 'rstacruz/sparkup' , { 'rtp' : 'vim/' }
有舊插件的情況下,下載新的插件并重命名以避免沖突
Plugin 'ascenator/L9' , { 'name' : 'newL9' }
|
下載方式除了在vim中運行:PluginInstall外,還可以在命令行中運行: 其它常用的命令: 1 2 3 4 | :PluginList - lists configured plugins
:PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
:PluginSearch foo - searches for foo; append `!` to refresh local cache
:PluginClean - confirms removal of unused plugins; append `!` to auto - approve removal
|
YouCompleteMe非常好用的自動補(bǔ)全插件,就是比較重。 官網(wǎng)地址:http://valloric./YouCompleteMe/ github地址:https://github.com/Valloric/YouCompleteMe YouCompleteMe安裝后還需要手動編譯,然后再在.vimrc中配置。 在ubuntu中使用,首先準(zhǔn)備一些工具: 1 | sudo apt - get install build - essential cmake
|
1 | sudo apt - get install python - dev python3 - dev
|
使用vundle安裝: 1 | Plugin 'Valloric/YouCompleteMe'
|
編譯: 1 2 | cd ~ / .vim / bundle / YouCompleteMe
. / install.py - - clang - completer
|
參數(shù) --clang-completer是為了加上C系列語言的自動補(bǔ)全,也可以不加: 1 2 | cd ~ / .vim / bundle / YouCompleteMe
. / install.py
|
耐心等待吧,要花很長時間... 復(fù)制一下默認(rèn)配置文件到用戶主目錄: 1 | cp third_party / ycmd / examples / .ycm_extra_conf.py ~ /
|
YCM常用的一些選項,可根據(jù)個人喜好調(diào)整: 1 2 3 4 | let g:ycm_min_num_of_chars_for_completion = 2 '開始補(bǔ)全的字符數(shù)'
let g:ycm_python_binary_path = 'python' 'jedi模塊所在python解釋器路徑'
let g:ycm_seed_identifiers_with_syntax = 1 '開啟使用語言的一些關(guān)鍵字查詢'
let g:ycm_autoclose_preview_window_after_completion=1 '補(bǔ)全后自動關(guān)閉預(yù)覽窗口'
|
代碼跳轉(zhuǎn): 1 | nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
|
開關(guān)YCM: 1 2 | let g:ycm_auto_trigger = 0 'turn off
let g:ycm_auto_trigger = 1 'turn on
|
支持vim8的補(bǔ)全插件YouCompleteMe實際上是使用jedi-vim來補(bǔ)全python代碼的,如果覺得YCM實在太重,可以使用支持vim8的maralla/completor.vim來補(bǔ)全代碼: 下載: 1 | Plugin 'maralla/completor.vim'
|
下載jedi: 配置: 1 | let g:completor_python_binary = '/path/to/python/with/jedi/installed'
|
設(shè)置起來比YCM簡單很多了。 自動縮進(jìn)插件寫python代碼,自動縮進(jìn)是必須的,可以使用indentpython.vim插件: 1 | Plugin 'vim-scripts/indentpython.vim'
|
語法檢查安裝syntastic插件,每次保存文件時Vim都會檢查代碼的語法: 1 | Plugin 'vim-syntastic/syntastic'
|
添加flake8代碼風(fēng)格檢查: 運行F7就可以進(jìn)行flake8檢查了。 配色方案solarized配色方案已經(jīng)流行很久了,github地址https://github.com/altercation/vim-colors-solarized。 手動下載: 1 2 3 | $ cd ~/.vim /bundle
$ git clone git: //github .com /altercation/vim-colors-solarized .git
$ mv vim-colors-solarized ~/.vim /bundle/
|
或者vundle下載: 1 | Plugin 'altercation/vim-colors-solarized'
|
solarized有dark和light兩種配色,配置: 1 2 3 | syntax enable
set background=light or dark
colorscheme solarized
|
也可以根據(jù)gui模式和終端模式進(jìn)行切換: 1 2 3 4 5 | if has( 'gui_running' )
set background=light
else
set background=dark
endif
|
另外一種配色Zenburn方案: 1 | Plugin 'jnurmine/Zenburn'
|
兩種配色切換: 1 2 3 4 5 6 | if has( 'gui_running' )
set background=dark
colorscheme solarized
else
colorscheme Zenburn
endif
|
nerdtree給vim添加一個樹形目錄,地址https://github.com/scrooloose/nerdtree。 下載: 1 | Plugin 'scrooloose/nerdtree'
|
添加開關(guān)樹形目錄的快捷鍵: 1 | map <C-n> :NERDTreeToggle<CR>
|
Ctrl+n就可以開啟目錄了。 設(shè)置忽略.pyc文件: 1 | let NERDTreeIgnore=[ '\~$' , '\.pyc$' , '\.swp$' ]
|
為nerdtree添加git支持: 1 | Plugin 'Xuyuanp/nerdtree-git-plugin'
|
如果你想用tab鍵: 1 | Plugin 'jistr/vim-nerdtree-tabs'
|
vim-powerline美化狀態(tài)欄,可以顯示當(dāng)前的虛擬環(huán)境、Git分支、正在編輯的文件等信息。 1 | Plugin 'Lokaltog/vim-powerline'
|

indentLine縮進(jìn)指示線,地址https://github.com/Yggdroot/indentLine。 安裝: 1 | Plugin 'Yggdroot/indentLine'
|
開關(guān): python是靠代碼縮進(jìn)來判斷代碼塊的,縮進(jìn)指示線還是很方便的。 
vim-autopep8自動格式化工具,安裝后運行:Autopep8就可以自動依照pep8的標(biāo)準(zhǔn)自動格式化代碼。 地址https://github.com/Yggdroot/indentLine。 首先安裝autopep8: 1 | Plugin 'tell-k/vim-autopep8'
|
可以設(shè)置快捷鍵F8代替:Autopep8: 1 | autocmd FileType python noremap <buffer> <F8> :call Autopep8()<CR>
|
auto-pairs自動補(bǔ)全括號和引號等,地址https://github.com/jiangmiao/auto-pairs。 1 | Plugin 'jiangmiao/auto-pairs'
|
ctrlp.vim搜索插件,在vim normal模式下,按下ctrl+p,然后輸入你要尋找的文件就行了。 地址https://github.com/kien/ctrlp.vim。 ag.vim搜索引擎使用了the_silver_searcher 1 2 3 | apt- get install silversearcher-ag
or
brew install the_silver_searcher
|
插件 使用 1 | :Ag [options] {pattern} [{directory}]
|
vim-fugitivegit集成插件,可以在vim中運行g(shù)it命令,https://github.com/tpope/vim-fugitive。 1 | Plugin 'tpope/vim-fugitive'
|
|