vim folding

My favorite vim setting

// vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker syntax=php:

has the unfortunate side-effect of causing others to complain about the fact that my source code is “folded.” The problem is that some people who are only vim-users-only-when-working-on-a-remote-machine have a small learning curve when they see a page that looks like

<?php
// vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker syntax=php:
+-- 12 lines: docs -------------------------------------------------------------
+--  6 lines: imports ----------------------------------------------------------
+--154 lines: WidgetProviders --------------------------------------------------
?>

But with about a minute, training yourself how to navigate vim folding will pay itself off handsomely for those of us who don’t use IDEs or do fancy stuff like exuberant ctags.

[quick fold tutorial after the jump]

If you absolutely hate folds

Just type “zR”.

How I navigate folds

Try out these commands:

  • za toggle fold open/close
  • zo open fold under cursor (or zO to open them all). For instance, if there is a bug in line 68 then “68GzO” will jump you to where you want to be but still give you a good view of the rest of the code.
  • zc close fold
  • zR open all folds
  • zM close all folds

Folding is a marker so you can use the standard navigation tricks with them:

  • [z jump to beginning of fold
  • ]z jump to end of fold
  • zj jump forward one fold
  • zk jump backward one fold

Creating folds

I use explicit folding, aka “marker” folding where the marker is three right and three left braces. You can get fancier, but at that point it starts to look like an scintilla so I don’t bother.

You can read all about folding here.

Here is another tutorial.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.