2006-01-30

Vim stuff

Hah!

I often see other bloggers talking about vi and I want to jump up and down and demonstrate how to do what they're talking about, but without the pain of using vi-ish ways.

What Brad suggests doing in vi is something along the lines of:
  • Go to where you want the comment block to start
  • Hit mm
  • Go to where you want the comment block to end
  • Hit mn
  • Type :'m,'ns/^/#/
Which is archaic! Manaully doing marks like that is so 1980.

The two techniques that are possible in vim, which are both reasonably good, but I like the second one best are:
  • Go to where you want the comment block to start
  • Hit v
  • Go to where you want the comment block to end
  • Hit :s/^/#/
When in visual mode, pressing : will make vim automatically enter in the code that means "from the mark which is the start of the visual block to the mark that is the end of the visual block", or :'<,'>. This will essentially do the same thing as what Brad suggests, but using visual mode instead of manual marking.

The second technique, which as I mentioned, is my preferred one, is using Visual Block mode.
  • Go to where you want the comment block to start
  • Press Zero 0 so that you will be in the very first column
  • Press Control-v
  • Go to where you want the comment block to end
  • Press I#<esc>
This will insert the # (or whatever other text you wish to insert) at the beginning of each of those lines.

Even better, using the last technique I showed you, you can uncomment the same code by selecting the comments in the same way (using Control-v) and hitting x to delete them.

1 comment:

Anonymous said...

You are such a nerd!
Please don't talk about issues being 1980. I sit on my desk all day and wondering about computer stuff, and it simply doesn't make me feel very well.
yours, justus