Format code with vim

Source: Internet
Author: User

The Code pasted into Vim from other editors is often incorrectly formatted due to abnormal indentation. Found in Vim's official FAQ (http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl:

Format full text: Gg = G

Auto indent current row: =

This is an excerpt from the original article:

14.6. How do I Format/indent an entire file?

You can format/indent an entire file using the GG = g command, where

GG-goto the beginning of the file
=-Apply indentation
G-till end of File

For more information, read

: Help gg
: Help =
: Help g
: Help 'formatprg'
: Help C-indenting

 Original article addressHttp://hi.baidu.com/seesea8/blog/item/b96c8e51eb8f352743a75b41.html

Conversion:

Recently, when I visit csdn forums, I often have a problem-some users do not post the code in a standard format, and I often need to manually adjust the format when I post it to VIM, which is very troublesome. At this time, I miss the Alt + F8 ...... Think of VIM as the most intimate editor for programmers, and obviously won't ignore it. After some searching, I found some of the most commonly used simple skills and took notes for future use.
Vim format code:
Format full-text command Gg = G
Auto indent current line command =
Format the next 8 rows of the current cursor.
Format the selected row v select the code segment to be formatted =
Note:
Gg -- arrive at the beginning of the file
= -- Requires indentation
G -- until the end of the file
Code annotation: (this principle is to use Vim's regular expression replacement)
Take C ++ and python as examples.
Comment row:
Command Format: start line,Terminate a rowS/Character to be replaced/Replace with character/G 
For example,
Comment out 10 ~ in the C ++ source file ~ 20 lines, command:
: 10, 20 s/^ // G
Comment out 10 ~ In the Python source file ~ 20 lines, command:
: 10, 20 s/^/#/g
Cancel comments of consecutive rows:
The command format is the same as above. Only replacement characters and replacement with characters must be exchanged:
For example,
Cancel 10 ~ 20 lines of comments. The command is:
: 10, 20 s/^ ///g
Cancel 10 ~ 20 lines of comments. The command is:
: 10, 20 s/^ # // G


SELF: http://blog.csdn.net/ph123456789/article/details/6369018

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.