ThingyMaJig

Thingy Ma Jig is the blog of Nicholas Thompson and contains any useful tips, sites and general blog-stuff which are considered interesting or handy!

Connect

LinkedIn GitHub

Topics

announcement 25 apache 3 Apple 1 bash 8 code 7 cool 30 Days Out 8 Dark Basic Pro 4 design 12 doctor who 1 Drupal 74 E4600 1 EOS 400D 3 firefox 2 Flickr 3 free 21 games 5 geek 38 git 2 GreaseMonkey 1 hardware 7 Homebrew 1 How to 37 humour 5 iphone 1 javascript 1 jquery 1 K800i 6 k850i 4 lighttpd 3 linux 33 mac 9 miscellaneous 4 mobile phone 9 music 4 mysql 8 n73 1 n95 1 New Relic 1 Ogre3D 1 OS X 2 performance 3 photos 10 programming 40 Quicksilver 1 review 19 security 3 SEO 6 software 12 svn 2 technology 4 tip 7 tips 10 tv 3 video 3 vim 7 webdev 2 websites 33 wii 1 windows 1 YADS 10

Neat commenting in Vim

Posted on 12 January 2009 in
vim programming linux Drupal

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=
[adsense:468x60:4496506397]

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).