Vim can get confused when handling multiple nested fold markers.
The answer is to include a level (like this: "{{{1
), but I'm too 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. 🎉
Update: Inspired by kshenoy/vim-origami, I updated Chalk with the option to keep the markers aligned. My version aligns both the opening and closing markers and it also allows you to choose a "filler" character for the padding. On the other hand, it doesn't support "staggered" alignment and the markers are always aligned to the textwidth
setting.
-
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 let g:chalk_char = "." " The character used as padding " when aligning markers " Files for which to add a space between the marker and the current text au BufRead,BufNewFile *.vim let b:chalk_space_before = 1 " Create fold at visual selection vmap zf <Plug>Chalk " Create fold at operator movement nmap zf <Plug>Chalk " Create fold for specified number of lines nmap zF <Plug>ChalkRange " Create single (opening) fold marker at current level or specified count nmap Zf <Plug>SingleChalk " Create single (opening) fold marker at next levelor specified count nmap ZF <Plug>SingleChalkUp " Increment current fold level nmap =z <Plug>ChalkUp " Decrement current fold level nmap -z <Plug>ChalkDown " Increment levels in selection vmap =z <Plug>ChalkUp " Decrement levels in selection vmap -z <Plug>ChalkDown
(Choose 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).
Align all markers in the current document.
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 deeper than the current fold's.
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.
By default, Chalk will align your fold markers to the right according to your textwidth
setting. If you want to disable that behavior, set g:chalk_align
to 0
.
The character used to align markers to the right. The default is a space (" "
).
By default, Chalk will leave you in insert/replace mode after creating a new fold. You can disable this behavior by setting g:chalk_edit
to 0
.
By default, Chalk won't add a space between an existing line's text and the fold marker. This is good for a language like TeX, in which spaces make a difference, but may be undesirable for other languages, so you can change this behavior by setting b:chalk_space_before
to 1
for specific file types. E.g.: au BufRead,BufNewFile *.vim let b:chalk_space_before = 1
.
You may also be interested in my other plugins: