88
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
- 4,338
42
votes
Accepted
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 ...
- 47.7k
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 ...
- 9,039
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)=~'^""'?'&...
- 15.2k
21
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 ...
- 59.3k
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(...
- 15.2k
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 ...
- 29.9k
16
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. ...
- 59.3k
14
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 ~/....
- 17.2k
14
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 ...
- 17.2k
14
votes
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
- 477
13
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 ...
- 656
13
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 ...
- 2,039
12
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
...
- 59.3k
10
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 ...
- 8,222
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
- 14.3k
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 ...
- 6,586
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'
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 ...
- 29.9k
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/...
- 571
9
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 ...
- 3,678
9
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
- 214
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 ...
- 1,172
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:...
- 9,095
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.
...
- 15.5k
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 ...
- 14.3k
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
- 35.1k
8
votes
Accepted
Get rid of extra fold dashes when using Vim folding
You can
set fillchars=fold:\
See :h fillchars.
- 9,095
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)...
- 15.5k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
folding × 231vimrc × 24
syntax-highlighting × 20
vimscript × 15
key-bindings × 9
neovim × 9
autocmd × 8
highlight × 8
comments × 7
filetype-markdown × 7
filetype × 6
indentation × 5
foldtext × 5
search × 4
cut-copy-paste × 4
functions × 4
filetype-tex × 4
regular-expression × 3
gvim × 3
cursor-movement × 3
insert-mode × 3
visual-mode × 3
cursor-motions × 3
filetype-html × 3
performance × 3