Adventures in vim

Just ran across this question on Stack Exchange by DJ McMayhem, which pointed out a great tip for commenting blocks of code. Say you have

int main()
{
  int answer=42;
  string question;
  for(int i=0; i<answer; ++i) {
    question += "what do you get";
  }
  return answer;
}

and want to comment out the question. I used to do that by putting the cursor on line 4 (string question), hitting ma to save my place in mark a, moving down to line 7 (the end of the for loop), and doing :'a,./s/^/\/\//<Enter>.

Thanks to DJ McMayhem, I can now do that by starting on line 4, hitting v3j to select the lines in question, and doing :norm0i//<Enter>. Result:

int main()
{
  int answer=42;
//  string question; 
//  for(int i=0; i<answer; ++i) {
//    question += "what do you get";
//  }
  return answer;
}

Original: 20 keystrokes (counting Enter, but not counting Shift presses as separate keystrokes)

New: 13 keystrokes 🙂

My next adventure is to wrap :normal in a function so I can use :N or some such, for a savings of a further three keystrokes!

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.