Hot answers tagged

28

Yes, it is possible. A minimal setup would be: :set filetype=json :syntax on :set foldmethod=syntax


22

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)=~'^""'?'>'.(matchend(getline(v\:lnum),'""*')-2)\:'=' This will make any line starting with 2+ "'s a fold. The more "'s the deeper the fold. This allows you to sub divide sections if you need.


20

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 zo, or delete the fold with zd.


14

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 amount of whitespace + a comment character. Note that this is fairly naive, and may not work for all languages. So you may want to use a autocmd to be more specific: ...


13

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(v:lnum)),0,'')?'>1':'=' This looks more complicated than it is, because we can't easily use spaces in :set, but with spaces, and a newline or 2, it looks ...


12

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 lines, and content from the first (or all) lines). In your example, in Vim only the {...} parameter block itself is folded; the line above (that uses the ...


12

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 available see :help foldmethod


11

You can use :set foldcolumn=<number> to display a <number>-wide column that tries to visualize fold levels. It would look something like this, by default (with a marker fold method, using {{{ and }}}): - {{{ Top Level Fold |- {{{ Nested Fold ||- {{{ Deepest Fold ||| }}} || }}} |- {{{ Another Nested Fold || }}} | }}} However, ...


10

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 the quickfix window, and then performs either a <CR> or za accordingly. See: :h :map-expr :h 'buftype' :h expr1 :h string Mapping to a function You can ...


10

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 result of this evaluation controls what Vim considers a fold or not. Possible values are 0 the line is not in a fold 1, 2, .. ...


10

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 ~/.vim/after/ftplugin/html.vim: setlocal foldmethod=syntax This depends on having filetype plugin on in your ~/.vimrc, which you probably have. You can also ...


9

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


9

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. Of course, this is executed only when you are launching Vim, so you can use a autocommand like so: autocmd! BufReadPost * :if line('$') > 20 | set ...


8

Use a ! to force the writing, even if the file doesn't exist: :folddoopen .:w! >> file


8

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 customized folding this can work great: The example above shows possible categories that I find helpful to structure my .vimrc. It uses the following ...


8

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 to open a new buffer containing just that area. Any changes you :write in the new buffer will appear instantly in the original, and you can simply close it when ...


7

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 be using. You could use one of many external resources, however.


7

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/vim_options.vim "--- Here Be Functions! " (need to be sourced before stuff that uses 'em) runtime! functions/*.vim "--- Key Mapping source ~/.vim/config/...


7

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 by logical sections and subsections (or even deeper, if you feel brave), and splitting your config into several smaller files and :source-ing them. I ...


7

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 hide. Add this mapping to your ~/.vimrc : map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' \ . synIDattr(synID(...


7

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) function (…) Careful though, with this basic example, all () will be concealed, so a little more work must be done here, but it should be a good opportunity ...


7

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 'foldopen' *'foldopen'* *'fdo'* 'foldopen' 'fdo' string (default: "block,hor,mark,percent,quickfix, ...


6

The following seems to patch syntax folding onto the Pascal syntax that ships with Vim. Put this into ~/.vim/after/syntax/pascal.vim: syn clear pascalLabel pascalStatement pascalStruct syn keyword pascalLabel goto label syn keyword pascalStatement procedure function syn keyword pascalStatement program const var type if !exists("pascal_traditional") ...


6

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 mkview au BufRead NOTES.otl silent loadview augroup END What this means is that in your case you can handle manual folds using the :mkview and :...


6

You can set fillchars=fold:\ See :h fillchars.


6

So following the idea I suggested in comments here is what I've come up with: First let's demonstrate it with a gif (I'm not sure why the cursor leaves this ugly mark): The idea is to: Set the foldmethod to indent because it works well with python. (But I guess using marker or manual would work too) Open a split which will contain the "context information"...


6

Nice idea! Here's a basic function that achieves your goal using a different technique: function! FitOnPage() normal! zR while &foldlevel > 0 if line('w0') == 1 && line('w$') == line('$') break endif normal! zm endwhile endfunction It works by first expanding all the folds. It then closes folds (globally) until the ...


6

The foldopen setting controls which commands will open folds. In :help 'foldopen' we can see the list of possible values: item commands all any block "(", "{", "[[", "[{", etc. hor horizontal movements: "l", "w", "fx", etc. insert any command in ...


5

Vim already displays folds as a single line. However, in Vim's indent folding, all the lines that have the same indent are included in a fold. So in your screenshot, the lines you are referring to as "headers" (e.g., the one starting collection_base_url) are not within the folds. You can achieve something similar to Atom's folding by using Vim's foldexpr ...


5

You can read more about why this happens by looking at :help :syn-sync. The short answer is that vim decides to redraw the screen and starts from some point in the middle of the file. It incorrectly guesses the syntax at this starting point and it messes up the rest of the file. To fix this, I generally use this mapping, which I got from Steve Losh's ...


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