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:
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:
Once that’s done, folds can be created manually by typing
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:
Ugh – looks ugly…but it is effective. Here’s what it does:
- j = move the cursor up one line (to line 499)
- v = switch to visual block selection mode
- gg = move the cursor to the top of the document
- zf = create a fold
The end result? Everything above line 500 is folded out of the way. Very handy











Pingback: Code folding with VIM – the manual way | Jon's Blog | Drakz Free Online Service
Pingback: Code folding with VIM – the manual way | Jon's Blog | Drakz Free Online Service
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.