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

Leave a Reply

Your email address will not be published.