使用vim打造自己的python編輯器
基礎配置
vim的配置是在用戶主目錄下的 ~/.vimrc 文件中完成的,如果沒有的話,需要自己新建一下:
1 2 |
cd ~
touch .vimrc
|
首先做些簡單的配置:
1 2 3 4 5 6 7 8 9 10 |
set nocompatible "關閉與 vi 的兼容模式
set number "顯示行號
set nowrap "不自動折行
set showmatch "顯示匹配的括號
set scrolloff=3 "距離頂部和底部3行"
set encoding=utf-8 "編碼
set fenc=utf-8 "編碼 set mouse=a "啟用鼠標
set hlsearch "搜索高亮
syntax on "語法高亮
|
為py文件添加下支持pep8風格的配置:
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 "自動縮進 \ set fileformat=unix "保存文件格式
|
分割窗口
vim在編輯的時候就可以打開多個文件:
:vs 或者 :vsplit 將當前窗口豎直分割,並在上面新窗口中顯示當前文件
:vs filename 將當前窗口豎直分割,新文件在新窗口中顯示
:sp 或者:sv或者:split 將當前窗口水平分割,並在左邊新窗口中顯示當前文件
:sp filename 將當前窗口豎直分割,新文件在左邊新窗口中顯示
:new 新建文件並豎直分割
:vnew 新建文件並水平分割
如果想讓新窗口在右邊或者下方打開,添加配置:
1 2 |
set splitbelow set splitright
|
在窗口之間切換可以用鼠標,如果不想用鼠標,切換按鍵如下:
Ctrl-w-j
切換到下方的分割窗口Ctrl-w-k
切換到上方的分割窗口Ctrl-w-l
切換到右側的分割窗口Ctrl-w-h
切換到左側的分割窗口
覺得三個按鍵多的話可以設置快捷鍵:
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鍵了。
代碼折疊
當代碼行數很多的時候,代碼折疊是很必須的:
1 2 |
set foldmethod=indent
set foldlevel=99
|
使用zc按鍵來創建折疊,使用za來打開或者關閉折疊。
za經常會誤輸入,可以用空格鍵來替代za:
1 |
nnoremap <space> za
|
一鍵執行python代碼
如果想直接在vim中執行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代碼就可以自動執行了
插件
vim插件中最主要的就是vundle了,vundle用來管理vim的其它插件
Vundle
Vundle 是 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
|
如果想下載某個插件,比如自動縮進indentpython.vim插件,需要將
1 |
Plugin ‘vim-scripts/indentpython.vim‘
|
置於call vundle#begin()和call vundle#end()之間,保存配置後在vim中執行
1 |
:PluginInstall
|
即可以自動下載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 |
vim + PluginInstall + qall
|
其它常用的命令:
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
非常好用的自動補全插件,就是比較重。
官網地址:http://valloric.github.io/YouCompleteMe/
github地址:https://github.com/Valloric/YouCompleteMe
YouCompleteMe安裝後還需要手動編譯,然後再在.vimrc中配置。
在ubuntu中使用,首先準備一些工具:
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
|
參數 --clang-completer是為了加上C系列語言的自動補全,也可以不加:
1 2 |
cd ~ / .vim / bundle / YouCompleteMe
. / install.py
|
耐心等待吧,要花很長時間...
復制一下默認配置文件到用戶主目錄:
1 |
cp third_party / ycmd / examples / .ycm_extra_conf.py ~ /
|
YCM常用的一些選項,可根據個人喜好調整:
1 2 3 4 |
let g:ycm_min_num_of_chars_for_completion = 2 "開始補全的字符數"
let g:ycm_python_binary_path = ‘python‘ "jedi模塊所在python解釋器路徑"
let g:ycm_seed_identifiers_with_syntax = 1 "開啟使用語言的一些關鍵字查詢"
let g:ycm_autoclose_preview_window_after_completion=1 "補全後自動關閉預覽窗口"
|
代碼跳轉:
1 |
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
|
開關YCM:
1 2 |
let g:ycm_auto_trigger = 0 "turn off
let g:ycm_auto_trigger = 1 "turn on
|
支持vim8的補全插件
YouCompleteMe實際上是使用jedi-vim來補全python代碼的,如果覺得YCM實在太重,可以使用支持vim8的maralla/completor.vim來補全代碼:
下載:
1 |
Plugin ‘maralla/completor.vim‘
|
下載jedi:
1 |
pip install jedi
|
配置:
1 |
let g:completor_python_binary = ‘/path/to/python/with/jedi/installed‘
|
設置起來比YCM簡單很多了。
自動縮進插件
寫python代碼,自動縮進是必須的,可以使用indentpython.vim插件:
1 |
Plugin ‘vim-scripts/indentpython.vim‘
|
語法檢查
安裝syntastic插件,每次保存文件時Vim都會檢查代碼的語法:
1 |
Plugin ‘vim-syntastic/syntastic‘
|
添加flake8代碼風格檢查:
1 |
Plugin ‘nvie/vim-flake8‘
|
運行F7就可以進行flake8檢查了。
配色方案
solarized配色方案已經流行很久了,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
|
也可以根據gui模式和終端模式進行切換:
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‘
|
添加開關樹形目錄的快捷鍵:
1 |
map <C-n> :NERDTreeToggle<CR>
|
Ctrl+n就可以開啟目錄了。
設置忽略.pyc文件:
1 |
let NERDTreeIgnore=[ ‘\~$‘ , ‘\.pyc$‘ , ‘\.swp$‘ ]
|
為nerdtree添加git支持:
1 |
Plugin ‘Xuyuanp/nerdtree-git-plugin‘
|
如果你想用tab鍵:
1 |
Plugin ‘jistr/vim-nerdtree-tabs‘
|
vim-powerline
美化狀態欄,可以顯示當前的虛擬環境、Git分支、正在編輯的文件等信息。
1 |
Plugin ‘Lokaltog/vim-powerline‘
|
indentLine
縮進指示線,地址https://github.com/Yggdroot/indentLine。
安裝:
1 |
Plugin ‘Yggdroot/indentLine‘
|
python是靠代碼縮進來判斷代碼塊的,縮進指示線還是很方便的。
vim-autopep8
自動格式化工具,安裝後運行:Autopep8就可以自動依照pep8的標準自動格式化代碼。
地址https://github.com/Yggdroot/indentLine。
首先安裝autopep8:
1 |
$ pip install autopep8
|
1 |
Plugin ‘tell-k/vim-autopep8‘
|
可以設置快捷鍵F8代替:Autopep8:
1 |
autocmd FileType python noremap <buffer> <F8> :call Autopep8()<CR>
|
auto-pairs
自動補全括號和引號等,地址https://github.com/jiangmiao/auto-pairs。
1 |
Plugin ‘jiangmiao/auto-pairs‘
|
ctrlp.vim
搜索插件,在vim normal模式下,按下ctrl+p,然後輸入你要尋找的文件就行了。
地址https://github.com/kien/ctrlp.vim。
1 |
Plugin ‘kien/ctrlp.vim‘
|
vim-fugitive
git集成插件,可以在vim中運行git命令,https://github.com/tpope/vim-fugitive。
1 |
Plugin ‘tpope/vim-fugitive‘
|
使用vim打造自己的python編輯器