One extremely convenient feature of vim is its ability to fold
text. With this feature, you can make parts of a text file disappear, without
actually removing it. Think of it as getting unimportant stuff out of the way—a
real boon for developers who want to make functions disappear that aren’t important
to what’s being worked on at the moment.
In order to fold a section of text, place the cursor on the
line to start the fold and press V
when you’re in command mode (not in insert mode). Move the cursor to highlight
the selection of text you wish to fold. Once everything is highlighted, type zf in command mode, and the text will disappear
and be replaced by text like the following:
+-- 16 lines: function print_help()----------------------------------
To expand the fold, type zo
or use the right arrow key with the cursor on the folded line. The summary line
will be replaced with the hidden text. To re-close the text, type zc anywhere within the fold; vim
remembers the settings for the fold and will fold it appropriately.
Another neat feature here is that if you make changes to the
fold and add or remove text, vim still remembers the start and end markers and
will fold it accordingly. For example, if the original fold was 16 lines and
you added four lines, vim will fold all 20 lines, provided the added lines were
within the previously defined fold.
To get more help on vim’s folding features, type :help folding.
Another quick tip on vim is its abbreviation feature. With it,
you can create abbreviations for text. By typing this in command mode:
:abbr ap Apache Web Server
Anytime that you type ap[space]
in your document, it will automatically expand to the “Apache Web
Server”. This is a useful way of automatically correcting often misspelled
words, for example, :abbr hte the and
If you need to sort a block of text in vim, you can easily do
so by highlighting the text to sort (pressing [V] in command mode and moving
the cursor to select the text) and, when all the text to sort is highlighted,
typing !sort. You can do all kinds of
nifty things here because vim is using the external program “sort” as
a filter, so to do a reverse sort you would use !sort -r instead. You can use any text-filtering or transformation
program here that you like.
vim has more features than you can shake a stick at; these are
just a few samplings of how versatile vim really is.
Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!