Use Vim as a Python IDE
I love vim and often use it to write Python code. Here are some useful plugins and tools for building a delightful vim python environment, escpecially for Vim8:html
我喜歡vim,常常用它來編寫Python代碼。如下是一些有用的插件和工具,用於構建使人愉快的vim-python環境,尤爲是vim8:java
As you can see, tmux is also one of my favourite tools in terminal.python
如您所見,tmux也是我在終端中最喜歡的工具之一。c++
Syntax Checking
If you use Vim8, w0rp/ale is a better option than syntastic, for it utilizes the async feature in Vim8, you will never get stuck due to the syntax checking. It’s similar to flycheck in emacs, which allows you to lint while you type.git
若是您使用Vim8, w0rp/ale是比syntastic更好的選擇,由於它利用了Vim8中的異步特性,您永遠不會由於語法檢查而被卡住。它相似於emacs中的flycheck,容許您在鍵入時進行lint。github
(taken from ale)編程
Code Formatter
google/yapf can be used to format python code. Make a key mapping as bellow, then you can format your python code via <LocalLeader> =
.vim
能夠使用google/yapf格式化python代碼。將鍵映射設置爲bellow,而後能夠經過' = '格式化python代碼。bash
autocmd FileType python nnoremap <LocalLeader>= :0,$!yapf<CR>
You can also take a look at Chiel92/vim-autoformat.服務器
Sort Import
timothycrosley/isort helps you sort imports alphabetically, and automatically separated into sections. For example, use <LocalLeader>i
to run isort on your current python file:
timothycrosley/isort幫助您按字母順序對導入進行排序,並自動將其分紅幾個部分。例如,使用' i '在當前python文件上運行isort:
autocmd FileType python nnoremap <LocalLeader>i :!isort %<CR><CR>
Or you can use its vim plugin: fisadev/vim-isort.
Update: ALE now has a command ALEFix
for autofixing. Concerning code formatter and sort import, you could do that by merely configuring ALE properly. I’d love to put these in ftplugin/python.vim:
ALE如今有一個命令' ALEFix '用於自動修復。關於code formatter和sort import,您能夠經過正確配置ALE來實現這一點。我想把這些放到ftplugin/python.vim:
let b:ale_linters = ['flake8'] let b:ale_fixers = [ \ 'remove_trailing_lines', \ 'isort', \ 'ale#fixers#generic_python#BreakUpLongLines', \ 'yapf', \] nnoremap <buffer> <silent> <LocalLeader>= :ALEFix<CR>
If you want to fix files automatically on save:
若是你想修復文件自動保存:
let g:ale_fix_on_save = 1
Now you have the support of syntax checking and autofixing with one ALE! As a matter of fact, ALE also has a plan to support auto-completion via LSP. Keep watching this amazing project if you are interested.
如今,您已經支持語法檢查和自動修復與一個ALE!事實上,ALE還計劃經過LSP支持自動完成。若是你感興趣,請繼續觀看這個精彩的項目。
Auto Completion
Valloric/YouCompleteMe is a good way to provide code auto completion. It has several completion engines, aside from Python, C, C++, Rust, Go and Javascript are also supported. Whereas a bunch of people also think YCM is too huge and need to be compiled, then jedi-vim is an alternative. They all use jedi as their backend.
(from jedi-vim)
What’s more, I know many people use Shougo/deoplete.nvim. Thanks to the async API, some more hopeful completion plugins are borned:
maralla/completor.vim is an code completion framework for Vim8, and support NeoVim too.
roxma/nvim-completion-manager also provides experimental support for Vim8.
roxma/nvim-completion-manager 還爲Vim8提供了實驗支持。
prabirshrestha/asyncomplete.vim is a fork of nvim-completion-manager in pure vim script with python dependency removed.
prabirshrestha/asyncomplete.vim是純vim腳本中的一個nvim- completemanager分支,去掉了python依賴項。
(from NCM)
Update: Unfortunately, NCM is not maintained any more.
*更新:*不幸的是,[NCM](https://github.com/roxma/nvim-comple-manager/issues/12# issuecom-382334422)再也不維護了。
Update again: ncm2, the successor of NCM, comes out! coc.nvim is also promising.
Quick Run
If use Vim8, you can execute python file asynchronously by skywind3000/asyncrun.vim and output automatically the result to the quickfix window like this:
若是使用Vim8,您能夠經過skywind3000/asyncrun.vim異步執行python文件,並將結果自動輸出到quickfix窗口,以下所示:
" Quick run via <F5> nnoremap <F5> :call <SID>compile_and_run()<CR> function! s:compile_and_run() exec 'w' if &filetype == 'c' exec "AsyncRun! gcc % -o %<; time ./%<" elseif &filetype == 'cpp' exec "AsyncRun! g++ -std=c++11 % -o %<; time ./%<" elseif &filetype == 'java' exec "AsyncRun! javac %; time java %<" elseif &filetype == 'sh' exec "AsyncRun! time bash %" elseif &filetype == 'python' exec "AsyncRun! time python %" endif endfunction " Deprecated: " augroup SPACEVIM_ASYNCRUN " autocmd! " " Automatically open the quickfix window " autocmd User AsyncRunStart call asyncrun#quickfix_toggle(15, 1) " augroup END " " asyncrun now has an option for opening quickfix automatically let g:asyncrun_open = 15
For neovim, neomake/neomake is worthy of trying. Here is the description from neomake’s README:
對於neovim, neomake/neomake值得一試。如下是neomake的自述:
It is intended to replace the built-in :make command and provides functionality similar to plugins like syntastic and dispatch.vim. It is primarily used to run code linters and compilers from within Vim, but can be used to run any program.
Another approach is to use TMUX. The idea is simple: it can split your terminal screen into two. Basically, you will have one side of your terminal using Vim and the other side will be where you run your scripts.
PS: 另外一種方法是使用TMUX。這個想法很簡單:它能夠把你的終端屏幕一分爲二。基本上,終端的一端使用Vim,另外一端運行腳本。
Enhance the default python syntax highlighting
python-mode/python-mode provides a more precise python syntax highlighting than the defaults. For example, you can add a highlighting for pythonSelf
.
python-mode/python-mode提供了比默認值更精確的python語法高亮顯示。例如,您能夠爲「pythonSelf」添加高亮顯示。
hi pythonSelf ctermfg=68 guifg=#5f87d7 cterm=bold gui=bold
For more customized python syntax highlightings, please see space-vim-dark theme and syntax/python.vim in python-mode/python-mode . You can also put them after color command.
更多定製的python語法高亮顯示,請參見[space-vim-dark主題](https://github.com/liuchengxu/spacevim-dark/blob/aea40e6518a569911a63e9c41104d27e/colors/spacevim-dark.vim #L318-L337)和syntax/python。vim in python-mode/python-mode。你也能夠把它們放在顏色命令以後.
Actually, python-mode contains tons of stuff to develop python applications in Vim, e.g., static analysis, completion, documentation, and more. (But personally, I prefer to obtain the functionalities by some other better plugins.)
實際上,python模式包含了大量在Vim中開發python應用程序的內容,例如靜態分析、完成、文檔等等。(但就我我的而言,我更喜歡經過一些更好的插件來得到這些功能。)
Python text objects
vim-pythonsense provides text objects and motions for Python classes, methods, functions, and doc strings.
vim-pythonsense爲Python類、方法、函數和doc字符串提供文本對象和運動。
LSP
The concept of Language Server Protocol has been around for quite a while, many languages already have a decent LSP support. So far LSP is the only way to bring in various features similar to IDE for the text editors in a standard way. To do that, you need to install the correspoding language server and a LSP client to interact with it.
*Language Server Protocol的概念已經存在很長一段時間了,許多語言已經有了不錯的LSP支持。到目前爲止,LSP是以標準方式爲文本編輯器引入各類相似IDE的特性的唯一方法。爲此,您須要安裝correspoding語言服務器和一個LSP客戶機來與之交互。*
Vim LSP client | Implementation | Support |
---|---|---|
LanguageClient-neovim | Rust | vim/neovim |
ale | VimL | vim/neovim |
vim-lsp | VimL | vim/neovim |
neovim’s built-in LSP support | Lua | neovim only |
LCN implements the LSP client in Rust, so it obviously has an outstanding performance compared to others written in vimscript or lua. Most LSP clients are usable now, but far from perfect:
LCN在Rust中實現了LSP客戶機,所以與其餘使用vimscript或lua編寫的客戶機相比,LCN顯然具備出色的性能。大多數LSP客戶端如今都是可用的,但還遠遠不夠完美:
- simple and crude UI
- poor performance
Still a long way to go :).
Summary
There are also some neccessary general programming plugins, e.g.
也有一些必要的通用編程插件,例如。
- scrooloose/nerdcommenter for convenient commenter.
- Yggdroot/indentLine or nathanaelkane/vim-indent-guides for visually displaying indent levels in Vim.
- fzf and fzf.vim for fuzzy file searching, also vim-fz and fzy.
- ……
Although vim is great and many plugins are productive, IDE is still my first choice when it comes to refactoring code and debugging:). Some useful links for debugging python:
For detailed vim configuration, please refer to space-vim. Enable ycmd
/lsp
, auto-completion
, syntax-checking
, python
, programming
Layer , then you could get a nice vim environment for python like the above screenshot. Enjoy!
有關vim的詳細配置,請參閱space-vim。啓用ycmd/lsp、自動完成、語法檢查、python、編程層,而後您就能夠獲得一個適合python的vim環境,就像上面的截圖同樣。享受吧!