I have a long code file with syntax folding in Vim.
I know how to open all folds (zR) or close all folds (zM), and I know how to increase or decrease the foldlevel (zm, zr).
However when I increase the foldlevel the inner most folds are closed; instead I want the outer most folds closed while the inner most are unfolded. It is possible to do this manually by opening all folds and the closing each top level fold by hand it's incredible tedious specially with long files that I open quickly to get an overview of the code.
Is there any key shortcut to do this? Or do I need to make some sort of Vim function to do this? And if so, how?
I think you want to add set foldnestmax=1 to your $MYVIMRC.
Yes, you can type
:%foldc
Which closes one level of folds (outside in).
As Karl says, the foldnestmax setting is probably what you want.
zO (i.e., capital-letter-o) opens all nested folds.
I also find zx and zv very helpful.
When I use foldmethod=expr with a custom fold expression, I'll often modify the expression so that it only folds what I want it to fold.
Related
I'm using vim for LaTeX and I'm using latex-suite. It gives me nice syntax highlighting and folding, but in large files syntax highlighting gets "confused". If I open all folds, the syntax highlighting turns OK. I would like it to "just work" all the time though.
I seem to recall an option that would increase the number of lines that is used as basis for determining syntax highlighting but I cant find it.
I don't edit LaTeX, but perhaps you want ":syn sync fromstart"? Just be warned that this can significantly slow down Vim since it forces Vim to do syntax highlighting parsing for the whole file rather than a section of the file. See ::help :syn-sync".
Ctrl+L in normal mode forces a redraw and often fixes syntax colour problems.
zRzMzx (i.e., expand all folds, contract all folds, fold to show current line) sometimes fixes syntax highlighting problems related to folds
10 years later, this is still somehow an issue. Similarly as Jeromy, I suggest pressing zRzMzzza which stands for
open all folds
close all folds
open (toggle) the fold I'm on
center buffer on this line
It looks like we need to learn to live with this
I'm trying out code folding in vim but noticed it doesn't seem to mirror across pane splits when using scrollbind to achieve a "2-up" view of a long text/code file with relatively narrow lines (set up as per this guidance).
Is it possible to ensure a fold created in one pane is also created on the other?
I'm just getting familiar with the commands, but it'd probably be z+c (close a fold at the cursor, i.e. 'fold up') and z+o (open a fold at the cursor, i.e. 'unfold')
It strikes me that one use of code folding would be to let one of the split views be an "outline view" (fully folded up, and just used to give an indication of how far through the file the other pane is), but it'd be nice if I could get them fully synced for scrollbind.
nnoremap <Space> za<C-W><C-W>za<C-W><C-W>
in .vimrc allows you to toggle a folding simultaneously in two splits by pressing the space bar.
I opened a large javascript file in gvim and am trying to use folding, but I cannot get it to work
Im trying
zo - opens folds
zc - closes fold
zm - increases auto fold depth
zr - reduces auto fold depth
but it says folding does not exist?
Do I have to turn the folding mechanism on?
Why wont this work? Is it a new install of gvim?
The folding behavior depends on the values of a bunch of options. The most important is foldmethod which dictates how folds are calculated. You can see its value with :set foldmethod? which should tell you which of these methods, manual, indent, expr, marker, syntax, diff, is currently in use. Refer to :help 'foldmethod' and the linked help sections for details.
manual is the default method where folds have to be created manually with zf before being opened or closed.
With indent, folds are automatically calculated for you based on indenting, with syntax they are calculated based on the filetype-specific syntax rules.
In vim, you can create a fold with zf, so if is %, you can neatly fold a delimited block.
is there a way to select some text in visual mode and automatically fold that?
I've noticed that if I select a closed fold and use dd to delete it, then go somewhere else and use P to paste it, the fold will disappear and the fold will be expanded. I want to cut the folded stuff and paste it and have it paste with the folds still intact and closed.
You can do the first if you use :set foldmethod=manual and then simply highlight the block you want and use zf to fold it.
I don't think you can do this (at least, not without a script)
Vim has several different fold strategies; I assume you are talking about manual folding.
Unfortunately, yanking and pasting does not keep the fold in-tact. You may be able to create a script that will re-fold pasted text, but it seems like it would be more annoying than useful in the end.
I am in a project with 3 people. We need to have the same folds in Vim for each member. How can I share my folds?
[Feedback]
I understood one important thing: Google ignores signs, such as {{{, so please google "VIM three braces" to find help about the marker-method. It becomes much easier to practise, as you can quickly find relevant information.
In order to use the the marker-method (suggested by Adam Bellaire), please note that you have to set the method:
:set foldmethod=marker
Thanks for your answers!
Probably the easiest way is to just use fold markers (e.g. {{{1), making sure to include the vim:fdm=marker setting in the file itself. For example, here's a shell script which contains both the setting to use fold markers and two levels of fold:
#/bin/sh
# vim:fdm=marker
echo This file contains fold markers.
#Top Level Fold {{{1
echo This is a top-level fold.
#Second Level Fold {{{2
echo This is a second-level fold.
Opening this file in vim will show the first four lines and then a fold, which if expanded will reveal the second fold. Just make sure to put a space between your comment syntax and the vim:fdm=marker line or vim won't see it. For example, in C you could use:
// vim:fdm=marker
Folds in files ?
Well, same settings should result in same folds.
Vim can fold in several ways: manually, by indent, by expression, by syntax, and by markers (by default, I believe are curved brackets, 3 of them).
So if you have the same vim version, and they haven't changed their syntax and indent files, let them check out your vimrc for foldmethod and foldmarker options, and copy them to their vimrc files. That should do it.
I haven't shared VIM folds with someone before, but if you're working on the same machine perhaps you can use VIM sessions, which will save your current state (including folds). Run the following command in VIM:
mks! /path/to/session_file
Then your friend can load up the session file:
vim -s /path/to/session_file
Ancient history, I know, but this may have appeared since the version of vim present in '09, and since I don't have an adequate reputation to comment yet, here we go.
The good news is that saving a view for the file should save manual folds as well, even nested folds.
The bad news is that I found it didn't produce consistent results under vim 7.0 (RHEL 5.5). This may have been fixed in a subsequent update that, sad to say, we aren't allowed to install.