brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set foldmethod=indent lets Vim fold code by its indentation

Vim
~/tips/tip-vim-fold-indent

:set foldmethod=indent

Collapses indented blocks so you can see a file's outline.

In a long file I like to see only the outline: function names, top level keys. Vim can fold everything else away based on indentation.

:set foldmethod=indent

Every deeper level of indentation becomes a fold. Then a few keys do the work:

za
zM
zR

za toggles the fold under the cursor, zM closes all folds and zR opens them all. zo and zc open and close one fold, if you prefer explicit keys.

As soon as you set the option, Vim closes all folds, which can be a surprise. I’d put this in a vimrc so files open unfolded, and close what you want by hand:

set foldmethod=indent
set foldlevelstart=99

Indent folding works well for Python and YAML, and fine for most other code that is indented properly. Vim also has foldmethod=syntax, which uses the syntax rules of the language, and marker, which folds between {{{ and }}} in comments.

Folds only change what you see. Searching still finds text inside closed folds and opens them, but some commands, like dd on a closed fold, delete the whole fold at once.

Further reading: Vim help: fold-indent, Vim help: za

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link