Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim syntax folding with perl

Tags:

vim

perl

folding

I use vim at work for developing pretty sizable perl code, and I got stuck while trying to learn how to use folds properly.

(Note: I'm still relatively new to Vim - switched over from gedit about half a year ago, but I'm still learning the new powerful things everyday!)

This is my portion of .vimrc related to folding

" mouse is on
set mouse=a
" folding
set foldmethod=syntax
set foldlevelstart=1
let perl_fold=1
let sh_fold_enabled=1
let perl_extended_vars=1
let perl_sync_dist=250

So this thing is good in that it folds all the code, and I can freely open / close folds with simply pressing z-a. But it's also bad in that everything is folded when I open a file.

Is there a way to have the file not be folded when I open the file, but still allow me to open/close blocks of code based on perl syntax with z-a? (or some other keystroke)

like image 585
Joe Avatar asked Oct 30 '13 21:10

Joe


People also ask

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 do I collapse a line in Vim?

Manual Folding in visual mode (for example, hit V , then hit j multiple times to select how many lines you want, then zf to make them foldable).

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.

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.


1 Answers

The "fold level" is the depth of the fold:

1
  2
    3

The line that you put in your ~/.vimrc,

set foldlevelstart=1

tells vim fo close every fold up to level 1 by default.

Set it to an impossibly high value to open all folds by default:

set foldlevelstart=999

Note that you can also try a low level like 2 or 3 which may have interesting results depending on your coding style.

See :h foldlevelstart.

like image 102
romainl Avatar answered Sep 22 '22 08:09

romainl