Best practices for writing Python with Vim

Source: Internet
Author: User
Tags comparison table

Let's take a picture first:

For some small Python projects, using Vim is a good choice. What is the best practice for writing Python with Vim in the context of the self-informed answer in this article? , the following is a supplement to the old answer, in particular, some of which are mainly aimed at vim8. If you want more content, you can see some answers to that question.

Grammar check

If you use VIM8, then you can use the asynchronous detection of W0rp/ale instead of syntastic, no longer envy Flycheck, do not have to because of grammar check and Kaka.

About the ale this part of the personalized configuration, in fact, a little "nit-picking", the general use of the default should be enough, but I like fancy things, toss a. The things involved may be relatively minor, you can directly see Space-vim:syntax-checking to understand the approximate configuration. Mainly for the ale provided by the Statusline content to be re-extracted to show. If there is no syntax error, it is not displayed in Statusline, if there is warning or error, then the line is differentiated by different colors.

And on the side of the warning or error sign, I like to clean a little, with the front color distinction is good, the following is sometimes like a piece of patch affixed to the top, feel not very good-looking.

Syntax enhancement

Vim comes with a bit weaker for python highlighting, and even self doesn't light up for me, which can be enhanced by Python-mode/python-mode:

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

Specifically, you can see here Space-vim:python layer

In the Python-mode Syntax/python.vim, you can see the Syntax/python.vim more detailed than the vim of the highlighted group, should not be ugly to understand the content, according to the color table itself to modify the highlight style. This is terminal 256 color comparison table terminal colors and their 24bit equivalent (or similar), this is the GUI color table complete HTML True color Ch Art.

The advantage of doing this naturally is that you want the effect to be completely customizable, but there are drawbacks, because there may not be a way to fit all of the vim themes. When you switch themes, these custom highlighting for Python might not fit. Even the most popular of those vim theme, I am afraid there are two digits, some are cold tones, some are warm colors, some are mainly blue, some are red mostly, should not be too easy to find some matching color. But everyone's favorite subject may be just a few, so choose it yourself ....

In fact, python-mode a bit of a synthesis of the meaning, includes the use of vim to write Python a lot of features, interested in can try to rely on it "a trick to play all over the world." But at the moment I'm just liking this part.

Code formatting

This can be done by GOOGLE/YAPF, install the YAPF, as follows to <LocalLeader>= format the current file:

 autocmd FileType python nnoremap <LocalLeader>= :0,$!yapf<CR>
Auto-complete

This is because I mainly use vim, so the youcompleteme, the actual backend with the Jedi-vim already mentioned. If you do not like Ycmd, use Neovim to try using Deoplete.nvim.

Import Collation

Are all kinds of import seem a bit messy? Use Timothycrosley/isort to sort it out:

FileType python nnoremap <LocalLeader:%<CR><CR>
One-click operation

This can be enhanced by skywind3000/asyncrun.vim, to replace the previous one. In fact, there ! AsyncRun! are some small points of attention:

A lot of people running one-click may probably look like this:

..."!g++ % -o %<""!time ./%<"...

Can you just replace the ! above AsyncRun! ? The answer is no, if you want to achieve the previous effect, you should:

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

To link more than one command (Linux) or & Link multiple commands (Windows) with semicolons, you can see here.

Specific VIM configuration here: Space-vim (think good words can drop to GitHub point a star support ha), enable Ycmd, syntax-checking, programming, Python Layer will be able to achieve the above effect 。

Best practices for writing Python with Vim

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.