Skip to navigation.
Neat commenting in Vim

Neat commenting in Vim

12
Jan
2009

I am a bit of a "neat geek" when it comes to coding. Things should be indented neatly and should have well laid out comments! Why? Well… It's easy to do and in 6 months time when you look at your code you will appreciate it! It will also me even more appreciated by another developer in less than 6 months time ;-)

I recently was looking through some old code and the 1-line comments were in an inconsistent form, for example:

  1. //xxxx
  2. // xxxx
  3. //Xxxx
  4. // Xxx

I personally prefer the 4th coding style (with a space and initial capital), so I started googling around for a solution using Vim and Regular Expressions. I found the following two search/replace phrases worked perfectly:

  • :%s=\/\/\([^ ]\)=// \1=
  • :%s=\/\/ \([a-z]\)=// \u\1=

The first one does a search for anything containing a "slash-slash" followed by a non-space character. The non-space character is grouped so we can reference it in the replace. The replace phrase changes that match to a "slash-slash" followed by a space and finally the character that was matched in the group.

The second is VERY similar to the first, except we search for "slash-slash-space" followed by a group matching a lowercase a to z range. The replacement is "slash-slash-space" followed by the group match converted to upper case (\u is a Vim option for conversion to uppercase).

Script

i like this script:

http://www.vim.org/scripts/script.php?script_id=1218

you have some very very very handy scripts

mark your text in visual mode than use ,cs and the code will wrapped with /* */
or ,c" " and your line will be commented/uncommented, depending on the current state

Nice script!

That is a very thorough script! Thanks for the resource :)

It'd be nice if you asked before taking stuff from my site. Contact me at webmaster [at] thingy - ma - jig . co . uk

This site was based on the Cobalt 2.0 Theme for phpBB written by Jakob Persson

Sponsors
Recent comments
RSS Feed
Search
Follow Us
Weblinks

Add to Technorati Favorites

TGC Webring

CMS Drupal Showcase

Feedburner for ThingyMaJig

View Nicholas Thompson's profile on LinkedIn

Nicholas Thompson's ClaimID

IconBuffet

Twitter

Become A Fan

bile-edge