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

How do I define custom fold marker strings instead of the default {{{ }}} in Vim?

Answer

:set foldmarker=region,endregion

Explanation

When using foldmethod=marker, Vim looks for the strings in foldmarker to identify fold boundaries. The default is {{{,}}} — but these conflict with CSS, shell arithmetic, and other contexts. You can set any pair of distinct strings as your fold markers using :set foldmarker=START,END.

How it works

  • :set foldmethod=marker — enables marker-based folding
  • :set foldmarker=START,END — sets the open and close marker strings
  • A number appended to a marker sets the fold level: region1, region2 etc.
  • :setlocal foldmarker=... applies the setting to the current buffer only

Example

For Python files, use region/endregion markers that match IDE conventions:

:setlocal foldmethod=marker
:setlocal foldmarker=region,endregion

Now you can write:

# region  <--- opens fold
def helper():
    pass
# endregion  <--- closes fold

For HTML files, use comments:

:setlocal foldmarker=<!--fold,fold-->

Tips

  • Put :setlocal foldmethod=marker | setlocal foldmarker=region,endregion in an autocmd FileType python block in your vimrc to apply automatically
  • You can embed the open marker with a level digit to create nested folds: # region1, # region2
  • To hide the markers, pair with :set conceallevel=2 and a conceal syntax rule
  • :set foldmarker? shows the current value; :set foldmarker& resets to the default {{{,}}}

Next

How do I make the = operator use an external formatter instead of Vim's built-in indentation?