Best practices for writing Python with Vim

Source: Internet
Author: User
Using vim is a good choice for some small Python projects. What is the best practice for writing Python with Vim in Zhihu's answer ?, The following content is a supplement to Zhihu's old answer, especially for vim8. for example... let's take a picture first:

Syntax enhancement

Vim's built-in highlighting of python is a little weak, and even self is not highlighted by me. this can be enhanced through python-mode/python-mode:

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

For details, refer to space-vim: python layer.

In the python-mode syntax/python. in vim, we can see syntax/python that comes with vim. vim's more detailed highlight group should not be difficult to understand the content, just modify the highlight style according to the color table. This is the terminal 256 Color table 256 Terminal colors and their 24bit equivalent (or similar). This is the GUI Color table Complete HTML True Color Chart.

The advantage of doing so is that the desired effect is completely customizable, but there are also disadvantages, because it may not be able to adapt to all vim themes. When you switch the topic, these custom highlighting for python may not match well. Even those popular vim themes, I am afraid there are two digits. Some are cold, some are warm, some are blue, and some are red, it is not easy to find some versatile colors. However, each person's favorite theme may be just a few theme, so choose one .....

In fact, the python-mode is a little more important, including many functions for writing python using vim. if you are interested, you can try to use it to "beat the world with one trick ". However, at present, I only like this part.

Code formatting

This can be done through google/ykp, and ykp is installed, as shown below: = You can format the current file:

autocmd FileType python nnoremap 
 
  = :0,$!yapf
  
 
Auto-completion

This is because I mainly use vim, so YouCompleteMe is used. the actual backend uses jedi-vim. if you do not like ycmd, you can try deoplete with neovim. using M.

Import sorting

Are various imports messy? Use timothycrosley/isort to sort it out:

autocmd FileType python nnoremap 
 
  i :!isort %
  
   
  
 
One-click operation

This can be enhanced through skywind3000/asyncrun. vim.!ChangeAsyncRun!Just fine. In fact, there are some small points of attention:

The one-click operation may take a long time like this:

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

You can directly!ChangeAsyncRun!? The answer is No. if you want to achieve the previous effect, you should do this:

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

Use a semicolon to link multiple commands (linux) or multiple commands (windows). For details, refer to here.

Complete vim configuration is here: >>> space-vim (if you think it is well written, click a star on github.

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.