Code folding with VIM – the manual way

Now that I’ve been using VIM (GVIM, to be exact) for the last few months exclusively as my code editor, I’m finally becoming comfortable with the some of the more common keyboard commands.  For example, I was editing an unordered list yesterday and wanted to fill it will a bunch of items.  Without thinking, I put my cursor onto the list item I wanted to copy and typed:

yy10p

What did that do?  Copied the current line and pasted it in 10 times.  How many keystrokes would YOU have had to use in your editor?  A lot more, I’m guessing ;)

Anyway, code folding is a must-have when I’m editing.  When the HTML code is particularly long, I like to fold stuff out of the way so I can focus on what I’m working on rather than see all the surrounding document.  Rather than rely on indentation or syntax, it’s faster for me to use the ‘manual’ folding method.  You can temporarily set this mode on the current buffer with the following command:

:set fdm=manual

Once that’s done, folds can be created manually by typing

zf

Let’s say I’m working on a large HTML file and I’m on line 500.  I want to fold everything above where I’m working.  There are several ways to do this, but here’s how I did it today.  Assumption: my cursor is already on line 500:

jvggzf

Ugh – looks ugly…but it is effective.  Here’s what it does:

  1. j = move the cursor up one line (to line 499)
  2. v = switch to visual block selection mode
  3. gg = move the cursor to the top of the document
  4. zf = create a fold

The end result?  Everything above line 500 is folded out of the way.  Very handy :)

About Jon Trelfa

I live in Detroit, Mi.
This entry was posted in Code and tagged . Bookmark the permalink.

3 Responses to Code folding with VIM – the manual way

  1. Pingback: Code folding with VIM – the manual way | Jon's Blog | Drakz Free Online Service

  2. Pingback: Code folding with VIM – the manual way | Jon's Blog | Drakz Free Online Service

  3. Michael says:

    I was using Vim the other day and was thinkin “Man I wish Vim had a way to fold code”. Your post was REALLY helpful. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>