Home » Linux, programming

Vim Formatting C/C++ Code

By: Zhiqiang Ma On: Mar 13, 2010 Views: 58 No Comment Print Email
Tags: , , , , ,

Vim provides some basic formatting commands. A combination of these commands with the editing commands will make the programmer happy.

A list of basic text formatting commands in Vim:

  • = is an operator that formats/indents text.
  • i{ is a text object that specifies the surrounding code block. It should be used together with v, =, etc.

Some basic but useful operation that are combination of the two basic ones above:

  • vi{ visually selects the inner code block around the cursor.
  • =i{ formats the code block.
  • =2i{ formats the current block and the block around it.
  • == formats current line.

The method to format a block of C/C++ or Java code:

  • First, go to the start point of codes to be formatted, then press v to start selection.
  • Second, go to the end point.
  • Third, press = to format the codes that have been selected. All braces and indentations will be aligned.

A short way to format the whole buffer:

  • gg=G

Read more:

Digg del.icio.us Stumble Techorati Facebook Newsvine Reddit Twitter
Mixx LinkedIn Google Bookmark Yahoo Bookmark MySpace LiveJournal Blogger RSS feed
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.