Tag Archives: Vim

Emacs versus Vim

Ten years ago I was a fanatical Emacs user. I knew it was so much more than just a text editor. I laughed at vi users, because vi was really a joke.

But vi improved over the years, and eventually I left Emacs and choose Vim as my preferred editor. Vim isn’t as powerful or elegant as Emacs (and for beginners, it has a much steeper learning curve), but it was better at two things I really wanted: syntax highlighting and Unicode support.

Emacs had syntax highlighting; it’s called /font lock/. Emacs also has a very smart automatic indentation system that is better than the one in Vim. But the font locking and auto-indenting are much slower in Emacs than they are in Vim. If I were an Emacs user now, I suppose I wouldn’t switch because my machine are so much faster than they were. So, I thought I would try Emacs again, and maybe switch back.

But the Unicode support I wanted wasn’t there. I don’t want anything too strange: I just want to be able to type some Japanese in UTF-8, but Emacs doesn’t like it. So it looks like I’ll be a Vim user for quite a while longer.

Help me, vim

Have you ever been editing a script or program in vim and had to open a man page for a command, function, class, or Perl module you’re trying to use? If so, you should use the K command to do it for you. Move your cursor over the command word, hit K, and vim will display the man page for you.

Move my cursor

Cursor movement in most editors (ed users can stop now) is a fundamental operation. In Vim (and old vi) cursor movement commands can be used in combination with other commands.

Most users know that dd deletes a line, but many do not know that it is actually a simplified form of the d command that deletes whatever you define with a cursor movement operation. I’ll explain with examples:

  • l (and the right-arrow key) moves one character right, so dl deletes the character under the cursor (since the cursor position is actually the left hand edge of the visible cursor), but x also deletes to same character, so use that instead;
  • w moves to the start of the next word, so dw deletes to the start of the next word;
  • ^ moves to the start of the line, so d^ deletes to the start of the line;
  • $ moves to the end of the line, so d$ deletes to the end of the line, but D also deletes to the end of the line, so use that instead;
  • G moves to the end of the file, so dG deletes to the end of the file;
  • { and } move back and forward paragraphs, so d{ deletes a paragraph backwards and d} deletes a paragraph forwards;
  • t moves forward (within a line) until it finds a character you specify, so tp will move forward until it finds a p (it won’t move if there is no p), so dtp will delete up to the p;
  • ' moves to the start of the line containing the mark you specify (you do know how to make marks, don’t you?) and ` moves to the actual mark position, so d'a will delete to the start of the line you marked with a and d`o will delete to the o mark;

There are too many other cursor movement commands for me to show, and there are many other commands that can use them. Of these, my next 2 favourites are y and =. y is the copy equivalent to the cut of d, and I’m assuming you know that p is paste. = is the smart indent command.

Make your mark

Making marks in Vim (and old vi) allow you to find particular places in your file without having to scroll or remember line numbers: just remember a letter. For example, typing ma will mark your current position with the a mark; you won’t see anything, but Vim will remember. Now move somewhere else in your file and type mb to mark it as b (or replace b with your favourite letter).

Once you have marked positions, you can jump to them: 'a will jump to the start of the line you marked with a and `b will jump to the exact position you marked with b.

Origami with Vim

Imagine you have a sheet of A4 paper with some of your code printed on it. (You need a good imagination; maybe you should go and print some code now…) Now find the start of a block, any block. Carefully make a mountain fold just below the first line of you chosen block, then make a valley fold in the middle of your block. Now look at your code.

You chosen block has vanished, apart from the first line, but the rest of your code is still visible. Now, assuming you know what the missing block does, your code is easier to read because it fits on a smaller page. Wouldn’t it be great if you could fold your screen?

Vim can do this for you, without damaging any hardware.

Look at your (imaginary) sheet of paper again. Lift it up to eye level and look at its side: your fold is Z shaped; so vim uses the z character for folding commands:

  • zo opens a fold so you can see the hidden parts;
  • zc closes a fold, hiding its contents;
  • zf creates a fold that you mark with a cursor movement;

For all the details, see :help folding in Vim.

If you’re using Perl and you want Vim to automatically make folds for your blocks and POD, add the following to your .vimrc:

  let perl_fold = 1
  let perl_fold_blocks = 1

Indenting code

Code indentation is a good idea (although probably not as good an idea as the Python people think), so Vim will indent your code for you.

First, put filetype plugin indent on in your .vimrc to enable the smart language-specific indentation algorithms (and other fancy features). If you don’t you’ll be using the simple old-vi-compatible indentation.

That’s mostly it. As you edit, vim will indent.

If you need to reindent an entire file, goto the first line and type =G in non-insert mode.