Let’s start with a picture:





screenshot

For some small Python projects, using Vim is a good choice. What are the best practices for writing Python with Vim? , the following content is a supplement to zhihu’s previous answers, especially some which are mainly aimed at VIM8. For more, check out some of Zhihu’s other answers to this question.

Syntax checking

If you use VIM8, you can replace Syntastic with w0RP/ALE for asynchronous checking. No more flyCheck envy and no more syntastic delays.

As for the ALE part of the personalization, it’s a bit of a nitpick, and generally the default should be enough, but I like the Fancy stuff, so I fiddled with it. While the details may be minor, you can look at space-vim: syntax-checking to get an overview of the configuration. Statusline content provided by ALE is mainly extracted and displayed. If there are no syntax errors, they are not displayed on the statusLine. If there are warning or error, they are colored differently.





ale statusline

As for the warning or error sign on the side, I like it to be clean, and it is better to distinguish it by the foreground scenery. Sometimes, it will be like patches pasted on the side, which is not very nice.





error/warning sign





error/warning sign

Syntax enhancements

Vim’s built-in highlighting for Python is a bit weak, and even self won’t highlight it for me. This can be enhanced by python-mode/python-mode:

hi pythonSelf            ctermfg=174 guifg=#6094DB cterm=bold gui=boldCopy the code

See space-vim: Python Layer here

Vim: syntax/python.vim: syntax/python.vim: syntax/python.vim: syntax/python.vim Terminal colors and their 24bit equivalent (or similar) This is the Complete HTML True Color Chart of the GUI.

The advantage of this, of course, is that the desired effect is completely customizable, but there is also a downside, as it may not fit all viM themes. When you switch themes, these Python custom highlights may not fit well. Even if it is only the popular vim theme, I am afraid there are double digits, some are cool colors, some are warm colors, some are mainly blue, some are red, it should be not easy to find some versatile matching colors. However, there are only a few topics that everyone likes, so choose…..

Python-mode is a bit of a one-trick trick. It contains a lot of python functionality that you can use to write python using Vim. But, for now, THAT’s just the part I like.

Code formatting

This can be done with Google /yapf, install yAPf, and

= will format the current file as follows:

 autocmd FileType python nnoremap <LocalLeader>= :0, $! yapf<CR>Copy the code

Automatic completion

This is because I’m mainly using Vim, so I’m using YouCompleteMe, while the actual backend is the jedi-Vim already mentioned. If you don’t like ycmd, those using Neovim can try using deoplete.nvim.

The import arrangement

Don’t the various imports seem a bit messy? Use timothycrosley/isort to sort things out:

autocmd FileType python nnoremap <LocalLeader>i :! isort %<CR><CR>Copy the code

A key operation

This can be enhanced with Skywind3000 / Asyncrun.vim to replace the previous! For AsyncRun! There you go. There are a few small caveats:

For many people a one-click run might look something like this:

.exec ! "" g++ % -o %<"
exec ! "" time ./%<".Copy the code

You can just put the top one! For AsyncRun! ? The answer is no. If you want to achieve the same results, you should:

exec "AsyncRun! g++ -stdc++11 % -o %<; time ./%<"Copy the code

To link multiple commands with a semicolon (Linux) or ampersand (Windows), see here.

The specific VIM configuration is here: Space -vim enables the ycmd, synsyntax -checking, programming, python Layer to do this.