天天看點

vim配置python開發環境_Vim - 配置IDE一般的python環境

Vim 作為一個經典的編輯器,如果配置合适,可以成為一個編輯python腳本非常給力的工具。這篇文章主要目的是介紹如何打造一個強大的vim編輯環境。

第一部分:軟體安裝:

在終端中執行:

vim --version

得到:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Oct 5 2017 04:42:50)

MacOS X (unix) version

Included patches: 1-1175

Compiled by [email protected]

Huge version with MacVim GUI. Features included (+) or not (-):

+acl +find_in_path -mouse_sysmouse -tag_any_white

+arabic +float +mouse_urxvt -tcl

+autocmd +folding +mouse_xterm +termguicolors

+balloon_eval -footer +multi_byte +terminal

+browse +fork() +multi_lang +terminfo

++builtin_terms +fullscreen -mzscheme +termresponse

+byte_offset -gettext +netbeans_intg +textobjects

+channel -hangul_input +num64 +timers

+cindent +iconv +odbeditor +title

+clientserver +insert_expand +packages +toolbar

+clipboard +job +path_extra +transparency

+cmdline_compl +jumplist +perl/dyn +user_commands

+cmdline_hist +keymap +persistent_undo +vertsplit

+cmdline_info +lambda +postscript +virtualedit

+comments +langmap +printer +visual

+conceal +libcall +profile +visualextra

+cryptv +linebreak +python/dyn +viminfo

+cscope +lispindent +python3/dyn +vreplace

+cursorbind +listcmds +quickfix +wildignore

+cursorshape +localmap +reltime +wildmenu

+dialog_con_gui +lua/dyn +rightleft +windows

+diff +menu +ruby/dyn +writebackup

+digraphs +mksession +scrollbind -X11

+dnd +modify_fname +signs -xfontset

-ebcdic +mouse +smartindent +xim

+emacs_tags +mouseshape +startuptime -xpm

+eval +mouse_dec +statusline -xsmp

+ex_extra -mouse_gpm -sun_workshop -xterm_clipboard

+extra_search -mouse_jsbterm +syntax -xterm_save

+farsi +mouse_netterm +tag_binary

+file_in_path +mouse_sgr +tag_old_static

...

這裡要确認兩件事:Vim的版本要求不低于7.3

要求支援python,確定清單中有+python

不同系統下Vim的安裝方式如下:

OSX:

安裝homebrew,然後在終端中執行:

brew update

brew install vim

*NIX:

在Debian或者Ubuntu上,執行:

sudo apt-get remove vim-tiny

sudo apt-get update

sudo apt-get install vim

Windows:

第二部分:配置檔案

vim的配置檔案是

~/.vimrc

你可以講需要的配置加入配置檔案中,重新啟動vim或者在normal環境中執行

:source %

便可加載配置檔案

編輯vimrc檔案,可以執行:

vim ~/.vimrc

有用的配置内容有:

set nocompatible

syntax on

filetype plugin indent on

set ic

set hlsearch

set encoding=utf-8

set fileencodings=utf-8,ucs-bom,GB2312,big5

set cursorline

set autoindent

set smartindent

set scrolloff=4

set showmatch

set nu

視窗移動:

vim的預設視窗移動方式是Ctrl+w Ctrl+hjkl,但是這樣太過複雜,對此進行綁定:

nnoremap

nnoremap

nnoremap

nnoremap

為python的特殊配置有:

let python_highlight_all=1

au Filetype python set tabstop=4

au Filetype python set softtabstop=4

au Filetype python set shiftwidth=4

au Filetype python set textwidth=79

au Filetype python set expandtab

au Filetype python set autoindent

au Filetype python set fileformat=unix

autocmd Filetype python set foldmethod=indent

autocmd Filetype python set foldlevel=99

自動執行:按一下F5,自動執行代碼:

map :call CompileRunGcc()

func! CompileRunGcc()

exec "w"

if &filetype == 'c'

exec "!g++ % -o %<"

exec "!time ./%<"

elseif &filetype == 'cpp'

exec "!g++ % -o %<"

exec "!time ./%<"

elseif &filetype == 'java'

exec "!javac %"

exec "!time java %<"

elseif &filetype == 'sh'

:!time bash %

elseif &filetype == 'python'

exec "!clear"

exec "!time python3 %"

elseif &filetype == 'html'

exec "!firefox % &"

elseif &filetype == 'go'

" exec "!go build %<"

exec "!time go run %"

elseif &filetype == 'mkd'

exec "!~/.vim/markdown.pl % > %.html &"

exec "!firefox %.html &"

endif

endfunc

第三部分:有用的插件

安裝插件強烈建議使用插件管理工具,這裡推薦的是vundle

首先安裝vundle:

git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

然後在vimrc中加入:

set nocompatible " required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

" alternatively, pass a path where Vundle should install plugins

"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required

Plugin 'gmarik/Vundle.vim'

" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)

" All of your Plugins must be added before the following line

call vundle#end() " required

filetype plugin indent on " required

關閉後重新打開vim,在normal模式下執行:

:PluginInstall

插件管理工具會自動的安裝配置檔案中寫到的插件。

在vimrc中加入

Plugin 'Chiel92/vim-autoformat'

關閉後重新打開vim,在normal模式下執行下述代碼即可:

Plugin 'Chiel92/vim-autoformat'

第四部分:推薦的插件:

1、YouCompleteMe

為了成為一個IDE,必須要有自動補全工具,這裡強烈推薦YouCompleteMe強烈建議按照官方說明一步一步進行安裝,史上最難安裝的vim插件并非名副其實

2、Autoformat

這個插件vim-autoformat能夠自動的一鍵格式化代碼

Before:

After:

安裝方式:

這款插件需要已經安裝有代碼格式化工具,安裝方式是在終端下執行:

pip install autopep8

推薦的配置是:

Plugin 'Chiel92/vim-autoformat'

nnoremap :Autoformat

let g:autoformat_autoindent = 0

let g:autoformat_retab = 0

let g:autoformat_remove_trailing_spaces = 0

每次在normal環境下按F6便可以格式化代碼

3、檔案樹

在vim中浏覽檔案夾:nerdtree

推薦配置是:

Plugin 'https://github.com/scrooloose/nerdtree'

nnoremap :NERDTreeToggle

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

通過按開關檔案樹

4、不同顔色區分括号比對

這個插件使用不同的顔色高亮比對的括号:rainbow_parentheses

Plugin 'kien/rainbow_parentheses.vim'

let g:rbpt_colorpairs = [

\ ['brown', 'RoyalBlue3'],

\ ['Darkblue', 'SeaGreen3'],

\ ['darkgray', 'DarkOrchid3'],

\ ['darkgreen', 'firebrick3'],

\ ['darkcyan', 'RoyalBlue3'],

\ ['darkred', 'SeaGreen3'],

\ ['darkmagenta', 'DarkOrchid3'],

\ ['brown', 'firebrick3'],

\ ['gray', 'RoyalBlue3'],

\ ['darkmagenta', 'DarkOrchid3'],

\ ['Darkblue', 'firebrick3'],

\ ['darkgreen', 'RoyalBlue3'],

\ ['darkcyan', 'SeaGreen3'],

\ ['darkred', 'DarkOrchid3'],

\ ['red', 'firebrick3'],

\ ]

let g:rbpt_max = 16

let g:rbpt_loadcmd_toggle = 0

au VimEnter * RainbowParenthesesToggle

au Syntax * RainbowParenthesesLoadRound

au Syntax * RainbowParenthesesLoadSquare

au Syntax * RainbowParenthesesLoadBraces

5、強大的狀态欄

這款插件提供了加強版的狀态欄:vim-airline

推薦配置:

Plugin 'https://github.com/bling/vim-airline'

6、代碼檢查工具

感謝丁春的評論:ale代替synatastic,有奇效

嘗試了ale,确實很不錯,它利用了vim8的異步處理功能,用起來不會有syntastic的卡頓現象

確定你的vim版本不低于8.0

安裝方式:

和syntastic一樣,它需要代碼檢測工具支援:

pip install flake8

推薦配置是:

Plugin 'w0rp/ale'

let g:ale_fix_on_save = 1

let g:ale_completion_enabled = 1

let g:ale_sign_column_always = 1

let g:airline#extensions#ale#enabled = 1

syntastic提供了自動代碼檢查功能,再也不用擔心變量寫錯了。

安裝方式:

這款插件需要已經安裝好代碼檢測工具,python代碼檢查工具的安裝方式是在終端下執行:

pip install flake8

推薦配置是:

Plugin 'scrooloose/syntastic'

set statusline+=%#warningmsg#

set statusline+=%{SyntasticStatuslineFlag()}

set statusline+=%*

let g:syntastic_always_populate_loc_list = 1

let g:syntastic_auto_loc_list = 1

let g:syntastic_check_on_open = 1

let g:syntastic_check_on_wq = 0

第五部分:推薦

它的功能是按一下F12,自動在所在行上方加入set_trace(),并且在檔案開頭加入相應的子產品導入

按F12之前:

按一下F12:

再按一下F12,就回到第一張圖的情況了

推薦配置:

Plugin 'sillybun/setbreakpoints_python'

autocmd FileType python nnoremap :call ToggleBreakPoint()

這是我實作的第二個插件,它實作了python自動格式化代碼功能,每次輸入完一條語句,上一條語句就會自動的調用autopep8進行格式化

它實作的功能是每寫完一條語句比如

a.num=a.num+1

在輸入完這條語句按下回車後,插件會自動的将它拍版成PEP 8标準的python風格代碼:

a.num = a.num + 1

安裝方式:

在配置檔案中放入:

Plugin 'sillybun/autoformatpythonstatement'

autocmd FileType python let g:autoformatpython_enabled = 1插件使用python3完成,需要vim的python3支援

我的vim配置檔案是vimrc