16

In the tcomment plugin, specifically in:

I'm seeing this at the end of some lines:

"{{{2

or

"{{{3

For example:

let g:tcomment#blank_lines = 2    "{{{2

Are these some sort of special comments? Or what?

Thanks.

1 Answer 1

42

"{{{2 is a foldmarker. Now, what are foldmarkers anyway? What are they useful for? And how do you use them?

First let's notice that the strings you mention in your question are composed of three parts:

  • " the comment character. This is specific to the filetype of your buffer, here it is " because it's a vimscript file. In a plain text file you could even omit any commenting character as this not a part of the actual marker.
  • {{{ the actual fold marker. :h 'foldmarker' tells us that {{{ and }}} are the default values but it could be any strings
  • 2 or any number which is optional represent the level of the marker.

These markers allow the user to have folds in their buffer, that is a way to define some sections in your buffer. You can then show or hide these sections and navigate between them. For example I use them in my vimrc to organize my plugins.

Here is what my vimrc looks like when all folds are closed:

enter image description here

Now if I open the " Plugins {{{ fold I can have a quick glance at the plugins I installed:

enter image description here

And one step deeper I can focus on the configuration of a particular plugin:

enter image description here

Note that I use both opening {{{ and closing }}} markers, this is a preference of mine but if I used levels I could simply omit the closing markers.


Now if you open the file you linked in vim you might be surprised to see no folding happening. This is because you need to set your foldmethod option to marker as :h 'foldmethod' says.

As the setting is local to a window you have several way to enable it. For example you could do it with an autocommand. You could enable the setting for any new window you open:

 augroup folding
     autocmd!
     autocmd WinNew * set foldmethod=marker
 augroup END

Or another common way to do it is by using modeline (:h modeline). These are these lines you often see at the top or at the bottom of vimscripts looking like this:

" vi: ft=vim:tw=72:fdm=marker

Here the modeline sets filetype, textwidth, and foldmethod for the buffer containing the file. This way next when you open the buffer the folds are enabled.


Some additional useful information when using folds:

  • :h fold-commands which shows you how to open them, close them and navigate between them.
  • :h 'foldmethod' for the different ways to create folds (markers are not the only option)
  • :h 'foldtext' to define how you want your folds to look like
7
  • 4
    Too bad the documentation, or at least an intro to it, isn't as clear and simple as what you said above. Thank you. This was helpful! Jul 15, 2020 at 8:54
  • So is this a bit like #region in Visual Studio? Jul 15, 2020 at 14:16
  • @htmlcoderexe Sorry, I'm not familiar enough with VS to give you a definitve answer on that
    – statox
    Jul 15, 2020 at 14:23
  • 2
    @htmlcoderexe Then indeed that seems to be the equivalent
    – statox
    Jul 15, 2020 at 15:17
  • 1
    OP already explained why the other answer is satisfying for them, no need to ask them to change the accepted answer :)
    – statox
    Jul 16, 2020 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.