5. Formating automatically

5.1. Restricting column width

One often has to restrict the column width to 80 or 75 or whatever. One can set this quite easily by using the command

	    :set textwidth=80
	 

To do this automatically just put the command in your .vimrc.

In addition to textwidth you may want the text to wrap at a certain column. Often such choices are dictated by the terminal one is using or it could just be by choice. The command for such a case is

	    :set wrapwidth=60
	 

The above command makes the text wrap at 60 columns.

5.2. Automatically indent code

While coding in C, one often indents inner-blocks of code. To do this automatically while coding, VIM has an option called cindent. To set this, just use the command

	    :set cindent
	 

By setting cindent, code is automatically beautified. To set this command automatically, just add it to your .vimrc

5.3. Comments

VIM also allows you to auto-format comments. You can split comments into 3 stages: The first part, the middle part and the end part. For example your coding style requirements may require comments to be in the following style

	    /*
	    * This is the comment
	    */
	 

In such a case the following command can be used

	    :set comments=sl:/*,mb:*,elx:*/
	 

Let me decipher the command for you. The commands has three parts. The first part is sl:/*. This tells VIM that three piece comments begin with /*. The next part tells VIM that the middle part of the comment is *. The last part of the command tells vim a couple of things. One that the command should end with */ and that it should automatically complete the comment when you hit just /.

Let me give another example. Lets say your coding guidelines are as follows

	    /*
	    ** This is the comment
	    */
	 

In such a situation you can use following command for comments

	    :set comments=sl:/*,mb:**,elx:*
	 

to insert a comment just type /* and hit enter. The next line will automatically contain the **. After you've finished the comment just hit enter again and another ** will be inserted. However to end the comment you want a */ and not **/. VIM is quite clever here. You don't need to delete the last * and replace it with /. Instead, just hit / and VIM will recognise it as the end of the comment and will automatically change the line from ** to */.

For more info hit :h comments