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:
As you can see, tmux is also one of my favourite tools in terminal.
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.
(taken from ale)
google/yapf can be used to format python code. Make a key mapping as bellow, then you can format your python code via
autocmd FileType python nnoremap <LocalLeader>= :0,$!yapf<CR>
You can also take a look at Chiel92/vim-autoformat.
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:
autocmd FileType python nnoremap <LocalLeader>i :!isort %<CR><CR>
Or you can use its vim plugin: fisadev/vim-isort.
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.
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.
Update: Unfortunately, NCM is not maintained any more.
If use Vim8, you can execute python file asynchronously by skywind3000/asyncrun.vim and output automatically the result to the quickfix window like this:
" 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:
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.
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
hi pythonSelf ctermfg=68 guifg=#5f87d7 cterm=bold gui=bold
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 text objects
vim-pythonsense provides text objects and motions for Python classes, methods, functions, and doc strings.
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.
|Vim LSP client||Implementation||Support|
|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:
- simple and crude UI
- poor performance
Still a long way to go :).
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
programming Layer , then you could get a nice vim environment for python like the above screenshot. Enjoy!