Hot answers tagged

83 votes
Accepted

How to fold text in a JSON file?

Yes, it is possible. A minimal setup would be: :set filetype=json :syntax on :set foldmethod=syntax
user avatar
  • 4,218
41 votes

What does three commented curly brackets ("{{{) at the end of a line mean? Is it just a comment?

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 ...
user avatar
  • 46.3k
30 votes
Accepted

Fold selected lines

You can press V to go into Visual Line mode, select the lines to be folded, then zf to make a manual fold. You'll need to :set foldmethod=manual if it isn't already set. You can open the fold with ...
user avatar
  • 8,779
25 votes

Is there a best practice to fold a vimrc file

I have the following modelines at the bottom of my vimrc which I copied from godlygeek, the author of tabular: "" vim:fdm=expr:fdl=0 "" vim:fde=getline(v\:lnum)=~'^""'?'&...
user avatar
17 votes
Accepted

Folding by regex search pattern

Do set foldmethod=expr and use 'foldexpr' to set a vim script expression that will define the fold start points. set foldmethod=expr set foldexpr=get(split(getline(v:lnum-1)),0,'')!=get(split(getline(...
user avatar
16 votes
Accepted

How to fold comments?

Folding only the comments is fairly easy by using foldmethod=expr: set foldmethod=expr foldexpr=getline(v:lnum)=~'^\\s*'.&commentstring[0] This will simply check if the line starts with any ...
user avatar
16 votes
Accepted

How to map Enter to custom command EXCEPT in quick fix?

Using an expression mapping is the easiest way to do this: nnoremap <expr> <CR> &buftype ==# 'quickfix' ? "\<CR>" : 'za' This defines a mapping that checks whether you are in ...
user avatar
  • 27.5k
13 votes
Accepted

How to write a fold-expr?

From :help 'foldexpr': It is evaluated for each line to obtain its fold level The foldexpr is evaluated, so it needs to be VimL code; there is no mention of "special syntax" or the like. The ...
user avatar
13 votes

HTML syntax folding in VIM?

The 'foldmethod' is a window-local option; setting it from your ~/.vimrc doesn't necessarily have the right effect. Because syntax folding is bound to the html filetype, these settings belong to ~/....
user avatar
  • 16.7k
13 votes
Accepted

Collapse vim folds to a single line, similar to Atom or Sublime Text

Two lines vs. one line In Vim, all lines within a fold will be collapsed to a single line, and the 'foldtext' option then determines the synopsis of those lines (usually dashes, the number of folded ...
user avatar
  • 16.7k
13 votes
Accepted

What does three commented curly brackets ("{{{) at the end of a line mean? Is it just a comment?

It's fold marker. Read more :h fold-marker
user avatar
12 votes

How do you fold functions automatically?

Given that your example is in Python, which relies on correct indentation of code blocks it is enough to base folding on that: :set foldmethod=indent For more information on the varieties of folding ...
user avatar
  • 646
12 votes

Is there a best practice to fold a vimrc file

It is a good idea to first define your own categories in your .vimrc (like a list with sublists and subsublists) and add all your plugins/settings/functions to the respective categories. Combined with ...
user avatar
  • 1,989
11 votes
Accepted

How to make jump to line automatically unfold

The foldopen setting controls which commands will open folds. In :help 'foldopen' we can see the list of possible values: item commands all any ...
user avatar
10 votes
Accepted

Folding fails: vim can't find any fold

Vim doesn't come with PHP syntax folding built-in. However, if all of your code is properly indented (as your example is), you can use a different fold method: :set foldmethod=indent
user avatar
  • 13.6k
10 votes

Hide certain lines completely (not folding)

I don't know how to achieve exactly what you want, only a part. You want to hide some text inside ~/.vimrc (for example). First we need to know which highlight group handles the text you want to ...
user avatar
  • 6,426
10 votes
Accepted

oneliner map to fold all / unfold all

So you want to toggle between all lines folded and no lines folded. Does this mapping do what you want? :nnoremap <expr> <f2> &foldlevel ? 'zM' :'zR'
user avatar
10 votes
Accepted

Is there a way to edit only part of a file?

You could try the terrific NarrwRgn plugin by our very own Christian Brabandt, which has always worked very well for me. Just select the lines you're interested in in visual mode, and then type :NR ...
user avatar
  • 27.5k
9 votes

Vimwiki: can't fold list items

Folding lists in VimWiki According to the documentation in vimwiki version 2.1 (the latest I found), there are a few fold methods, and of them only one works for folding list items. The setting is ...
user avatar
  • 8,032
9 votes

Is there a best practice to fold a vimrc file

I use my primary vimrc as a link to several other categorized files, sourcing each as it goes along, e.g. Vim options in one file, plugin settings in another. "--- Vim Options source ~/.vim/config/...
user avatar
  • 571
9 votes

Can I save folds?

I do exactly this for one file I refer to a lot NOTES.otl I performed the following in my .vimrc: " Useful for my Quick Notes feature in my tmuxrc augroup QuickNotes au BufWrite,VimLeave NOTES.otl ...
user avatar
  • 1,152
9 votes
Accepted

Folding slow with vimtex

First: Did you bother to read the vimtex documentation, i.e. :h vimtex-folding? The docs clearly point out that the fold-expr method of folding is slow, and so therefore provides an option g:...
user avatar
9 votes
Accepted

How can I enable folding only for files longer than a certain length?

You can use the line() function to fold or unfold your file: " In your vimrc if line('$') > 20 set foldlevel = 1 endif line('$') returns the line number of the last ($) line of your document. ...
user avatar
  • 15.2k
8 votes

How do you fold functions automatically?

Sure: set foldmethod=syntax You can then use zc to close a fold, zo to open one, or za to toggle. Unfortunately, Vim doesn't include folding information for Python by default, which you appear to ...
user avatar
  • 13.6k
8 votes
Accepted

save unfolded data to separate file

Use a ! to force the writing, even if the file doesn't exist: :folddoopen .:w! >> file
user avatar
  • 32.8k
8 votes

Is there a best practice to fold a vimrc file

You could say that "best practice" is preeminently a matter of opinion, :) but there are two approaches that (1) make obvious sense, and (2) can be applied to all config files, not only Vim's: folding ...
user avatar
  • 3,598
8 votes
Accepted

How to open a file with all folds opened?

zi inverts the option foldenable. But you don't want to invert it, you want to disable it. So put the following in your .vimrc: set nofoldenable
user avatar
  • 204
8 votes
Accepted

Is it possible to create a folding inside a single line?

You can hack around with some custom syntax and the use of the conceal: syntax region FunctionArguments start=+(+hs=e+1 end=+)+he=e-1 conceal cchar=… set concealleval=1 e.g. function (a, b, c, d, e)...
user avatar
  • 15.2k
8 votes
Accepted

Brace { } navigation without opening folds

Thanks to @romainl for this answer on super user. I couldn't have written this without their help! You can use the foldopen option to determine which set of motions will or won't open a fold. From :h ...
user avatar
  • 16.7k

Only top scored, non community-wiki answers of a minimum length are eligible