Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Vim folds?

Tags:

vim

folding

How can I export vim folds from a file, so that I can transfer it to a different machine?

For example say I create the folds in a file and save it on a local machine - where does the "folding" metadata go? Can I just copy it to another machine, or must I manually recreate the folds again?

EDIT: Hm, I've noticed this might be a duplicate question, but still there is not answer as to how to save the fold information WITHOUT modifying the source file.

Thanks

like image 844
Andriy Drozdyuk Avatar asked Sep 07 '10 16:09

Andriy Drozdyuk


People also ask

How do I save a fold in Vim?

The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.

How do I collapse a line of code in Vim?

Fold by default But, you can create a fold by z f {motion} in Normal mode or z f in Visual mode; e.g. z f j creates a fold for current line and the next following one in Normal mode.

How do I fold text in Vim?

If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines.

How does folding work in Vim?

Marker. Vim folds your code based on characters in the actual text. Usually these characters are put in comments (like // {{{ ), but in some languages you can get away with using something in the language's syntax itself, like { and } in Javascript files.


2 Answers

Save your session with the :mksession command, and restore it with vim -S Session.vim. The session file will restore almost everything, including folds (though changes to the file will mess it up).

That works if you must use manual folds, but it's really a lot easier to use one of the automatic fold methods — investigate indent, syntax, and expr, and look for syntax files that fully support folding.

like image 134
Josh Lee Avatar answered Sep 27 '22 23:09

Josh Lee


Ok I've figured out a relatively painless way of doing it. This does require some change to the source file however.

First, set the fold-marker to be java style /** comment **/ markers:

:set foldmethod=marker
:set foldmarker=/\*\*,\*\*/

then inside my source any time I want a fold I just type:

/** This is the title of the section */
Some stuff goes here blah
...
//the line below "ends" the fold by double star
/* **/

This produces folds just as I expect them and is much easier to deal with than the "dynamic" or visual folding.

like image 33
Andriy Drozdyuk Avatar answered Sep 27 '22 23:09

Andriy Drozdyuk