vimtricks.wiki Concise Vim tricks, one at a time.

How do I control which actions automatically open folds in Vim?

Answer

:set foldopen

Explanation

The foldopen option lets you specify exactly which cursor movements and commands will automatically open a closed fold. By default Vim is fairly aggressive about opening folds, but you can tune this to match your workflow — for instance, keeping folds closed during navigation but opening them only for search jumps.

How it works

foldopen accepts a comma-separated list of keywords. Vim opens a fold when the cursor enters it via any action in the list:

Keyword Triggers fold open when...
block Moving into a fold with {, }, [[, ]], etc.
hor Moving horizontally with l, w, etc.
mark Jumping to a mark with ' or `
percent Jumping with %
quickfix Moving via quickfix or location list
search Jumping to a search match (/, n, *, etc.)
tag Jumping to a tag with <C-]>, :tag, etc.
undo Cursor movement after u or <C-r>

Example

To prevent folds from opening during block navigation and horizontal motion, but still open them on search matches:

:set foldopen=search,tag,quickfix,undo,mark,percent

To keep all folds permanently closed until you explicitly open them with zo or za:

:set foldopen=""

To always open folds as soon as the cursor enters them (the most open behavior):

:set foldopen=all

Tips

  • Remove a single trigger without replacing the whole value: :set foldopen-=hor
  • Add a trigger: :set foldopen+=block
  • The default value is block,hor,mark,percent,quickfix,search,tag,undo — it covers almost everything
  • This setting is complementary to foldclose, which controls when folds automatically close

Next

How do I open just enough folds to see the current line without expanding everything?