How do I create custom fold regions with markers in my code?
Answer
:set foldmethod=marker
Explanation
Setting foldmethod=marker lets you define fold boundaries using special comment markers — {{{ to start a fold and }}} to end it. This gives you explicit control over exactly what gets folded.
How it works
:set foldmethod=marker
Vim looks for {{{ and }}} in your file (typically inside comments) and creates folds between them.
Example
" Configuration {{{1
set number
set relativenumber
set cursorline
" Key Mappings {{{1
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
" Auto Commands {{{1
autocmd BufWritePre * :%s/\s\+$//e
" }}}
The numbers after {{{ indicate fold levels — {{{1 creates a level-1 fold, {{{2 creates a nested level-2 fold.
Creating folds on the fly
" Select text and create a fold marker
zf " In visual mode, wraps selection with markers
" Or manually insert markers
" Start: {{{
" End: }}}
Custom markers
You can change the fold markers:
:set foldmarker=<<<,>>> " Use <<< and >>> instead
Tips
- Marker folding is ideal for vimrc files, configuration files, and long documentation
- Add fold level numbers for nested sections:
{{{1,{{{2,{{{3 - Use
zfin visual mode to quickly wrap a selection with markers - The
foldmethod=markersetting can be set per-file via modelines:" vim:foldmethod=marker