Here’s a neat little feature I’ve just been introduced to. After coding in VIM for so many years, one might think I’d know these things, but as it turns out, there’s a lot of features in VIM I’m not using. This one, however, will likely be a time-saver. There are a number of different code folding methods available in VIM (:help folding). I use the marker method by including this in my .vimrc file:

set foldmethod=marker

I can then use the following commands from command mode (not colon mode):

  • At the beginning of a function fragment, you can type ‘zfap’ to create a fold; this should add some {{{ }}} tags around your code in the comment of choice for the language you’re coding in.
  • You can type ‘zo’ to open a fold, or I can just hit the right arrow key on the folded code marker.
  • You can type ‘zc’ to close a fold.
  • You can type ‘zr’ to open all folds.
  • You can type ‘zm’ to close all folds.

More commands are available in :help folding.

 

2 Responses to Code folding in VIM 6.0+

  1. Aashish Dattani says:

    Hi,
    Any idea how you can get the highlight a particular pattern throughout the text when you search for it?

  2. Vibol says:

    There is a VIM set commmand:

    set hlsearch

    Which should turn on search pattern highlighting. Place that in your .vimrc file to turn it on permanently.

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>