Posts Tagged ‘vim’

Comment Multiple Lines in Vim

January 5th, 2010

Oh Look - The Vim LogoEarlier today I was asked how to comment out multiple lines in Vim. Unfortunately I wasn’t too sure what the best way was and the only one I knew felt less than friendly. Anyway – after some research I’ve nailed a few different methods and thought I’d post them here for people to choose their favourite one.

1. Visual Insert

This is the simplest method by far. Quick fire steps are:

  1. Ctrl + V (enter block-visual selection mode)
  2. Select the first character of the lines you want to comment out using “hjkl” or cursor keys
  3. Shift + i
  4. Type your comment marker: “//”, “#”, etc.
  5. Escape
  6. The end – there is a tiny delay as the comments are now put into your other lines.

2. Visual Search/Replace

This is similar to the above but has a slightly more arcane method to adding the comments.

  1. Shift + V (enter line selection mode)
  2. Select the lines you want to comment out.
  3. Type:
:s/^/# [Return]

or

:s/^/\/\/ [Return]

The first will replace the the beginning of the line (^) with a hash (#) and the second will do the same with a double slash (//). Note the extra slashes to escape.

3 Line Number Range

Similar to above this is the only method I could think of and its hardly very friendly but it gets the job done.

Pick the line numbers of the first and last lines you want commented – this is rarely simple.

Type:

:2,10s/^/#

That will put hash comments at the start of lines 2 to 10.

4. Using Markers

I’m not fond of this method as I’m still not 100% on markers. In fact I’m around about…..0% on markers as I’d not heard of them until now!! Woo for learning!

Anyway – markers. Oh yes, I’ll do a nice list of steps again:

  1. On the first line while in command mode type “ma” (no colon required here)
  2. Go to the last line of your intended block.
  3. Type:
:'a,. s/^/# /

In English that means: Form marker “a” to here (.) perform this command: s/^/# /

5. Plugins

You could just skip all that and install a Vim plugin. The advantage being they have more features. The disadvantage being you’d have to make sure all the servers you use have this plugin otherwise you’re stumped.

Vimrecover – Nice Gem

September 4th, 2008

Its easy to forget some of the best discoveries are made simply by browsing RubyForge.  This is now exception.  Its a nice little Ruby Gem to help Vim users to compare and manage their swp files.  These are produced as you’re editing a file and so if the session dies or there’s a crash then the swp is left behind for you to recover.  Problem being its not easy to determine the difference between the saved file ‘proper’ and the swp file.

That’s where this little gem comes in handy: vimrecover.

P.S. Sorry for overusing that damn Ruby logo but I dunno what else to do.

TextMate-like Vim Plugin

December 2nd, 2007

gVim LogoAfter the gushing praise I keep hearing about TextMate I installed a plugin for Vim which aims to give some of the benifits – though I’m yet to be convinced of its merits*.

Today when I was looking for extra bundles for the SnippetsEmu plugin (by far the most comprehensive) I came across a brand-new plugin by Taku Miyakawa called code-snippet. It’s not as mature or feature-rich as SnippetsEmu but as a newly released plugin its got a hell of a lot going for it. Here’s hoping it goes from strength-to-strength.

* Why? I’ll explain in a later post.