Vim can get confused when handling multiple nested fold markers.
The answer is to include a level (like this: "{{{1
), but I'm to lazy for that. Enter Chalk. It automatically adds the level to folds you create with zf
and increments all nested folds for you. There are also specific mappings and commands to increment and decrement markers while keeping them balanced and more. 🎉
-
Install Chalk using your favorite plugin manager or copy each file to its corresponding directory under
~/.vim/
. -
Add this to your
.vimrc
:set foldmethod=marker vmap zf <Plug>Chalk " Create fold at visual selection nmap zf <Plug>Chalk " Create fold at operator movement nmap zF <Plug>ChalkRange " Create fold for specified number of lines nmap Zf <Plug>SingleChalk " Open single fold at current level " or specified count nmap ZF <Plug>SingleChalkUp " Open single fold at next level " or specified count nmap =z <Plug>ChalkUp " Increment current fold level nmap -z <Plug>ChalkDown " Decrement current fold level vmap =z <Plug>ChalkUp " Increment levels in selection vmap -z <Plug>ChalkDown " Decrement levels in selection
(Chose the mappings you prefer. These are only suggestions.)
Increment or decrement the fold marker levels for the specified range (or the whole file, if no range is given).
This works just like Vim's zf
, creating a new fold at the current selection (in visual mode) or motion (in normal mode). The difference is that it also adds the fold level automatically according to where you're at.
This works just like Vim's zF
, creating a fold for the specified number of lines, but with the same perks as above.
The previous mappings add both the opening and closing fold markers. This one, on the other hand, adds a single opening marker at current line. If a count is specified before the mapping, that count is used as the level number. If no count is specified, this mapping will use the current level.
This mapping works like the previous one, but if no count is specified, it will use a level above the current fold.
When used in Normal mode, these mappings will increment or decrement the pair of markers (or the single marker, if you're into that sort of thing) at the current level.
When used in Visual mode, they'll increment or decrement every marker in the visual selection. Not that unlike their Normal mode counterparts the Visual mode mappings won't keep the opening and closing markers balanced unless they're both inside the selection.
You may also be interested in my other plugins: