[vi/vim tips] Indent codes in vi/vim editor

Have you ever experienced awful code arrangements after pasting the codes to vi or vim editor? Well, you shall worry no more! No matter how messed up was the codes you paste into the editor, you can indent them in a blink with the right command! Let’s see how it works:

  1. Make sure you are in viewing mode (not insert mode!)
  2. type the command below directly:
    gg=G
  3. BOOM! Indented! Happy coding!

(Explanation: “gg” makes the cursor go to the top of the file, “=” is a command to fix the indentation and “G” tells it to perform the operation to the end of the file.)

 

 

Reference:

http://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Advertisements

2 thoughts on “[vi/vim tips] Indent codes in vi/vim editor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s