15

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.

13

It's fold marker. Read more :h fold-marker

| improve this answer | |
  • 13
    I’m really tempted to downvote this because there’s such an opportunity lost—a better version of this answer would be tutorial and teach the op about fold markers and how they can used, rather than just give a name and a link – D. Ben Knoble Jul 15 at 2:59
  • 5
    @D.BenKnoble The whole "tutorial" is already in Vim's great help pages. Why reinvent the wheel? Better to redirect newbies to :help as it should be the primary source of information about Vim. Newbies just don't know what to search in there. – Jorengarenar Jul 15 at 3:22
  • 12
    Aaaaaand the link is throwing a 500 already :) The perfect example to why the useful content should be added here. – Matt Clark Jul 15 at 6:27
  • 6
    @Jorengarenar I agree with you on the fact that the help should always to the first source of information. However reformulating the help, aggregating its information in a synthetic manner and linking to the different related topics is usually a good way to help people ingest new information, that's basically the reason why this whole website exists. – statox Jul 15 at 8:18
  • 4
    Actually, at least for me, Jorengarenar gave me just exactly what I wanted and needed. The documentation on folding goes on and on and on, and there is no way it would be appropriate here to rehash it. However, in the future I may have another questions about it, because IMHO, it is far too complex for what it does. Perhaps I've just been spoiled by GUI folders. I don't know. Thanks @Jorengarenar. – Elliptical view Jul 15 at 8:47
38

As Jorengarenar said in their answer "{{{2 has to do with foldmarkers. 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
| improve this answer | |
  • 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! – Elliptical view Jul 15 at 8:54
  • So is this a bit like #region in Visual Studio? – htmlcoderexe Jul 15 at 14:16
  • @htmlcoderexe Sorry, I'm not familiar enough with VS to give you a definitve answer on that – statox Jul 15 at 14:23
  • @statox roughly, you put #region (something) before whatever you want to fold, #endregion after and then everything between becomes a collapsible [+] thingy in the margin, showing (something) when collapsed. Can even nest them, irreplaceable in GUI code... – htmlcoderexe Jul 15 at 15:00
  • 2
    @htmlcoderexe Then indeed that seems to be the equivalent – statox Jul 15 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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