Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Fold should I use in VIM?

I usually edit RUBY files in VIM. I want the methods(def...end) to fold. Could you please help me define the fold syntax?

like image 921
unj2 Avatar asked May 11 '09 17:05

unj2


People also ask

How does folding work in Vim?

Vim uses the same movement commands to define folds. Folding also works in visual mode. 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. Another option is to specify a range in command mode.

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 expand all lines in Vim?

To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold). (Side note: you may want to change the look of collapsed folds.

How do I fold a python code in Vim?

It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.


2 Answers

Assuming you've already got Ruby syntax highlighting setup and working, use the syntax mode for folding:

set foldmethod=syntax

This will give you folds on class .. end and def .. end, etc.

like image 186
overthink Avatar answered Sep 24 '22 22:09

overthink


I like to have everything fold by default, and this here will let you tweak a whole bunch of things related to folding. I do mostly Perl and C++ coding and I find it works well with that. Folding and unfolding is mapped to space key.

Here's what I have going in my vimrc:

  " Folding stuff
  hi Folded guibg=red guifg=Red cterm=bold ctermbg=DarkGrey ctermfg=lightblue
  hi FoldColumn guibg=grey78 gui=Bold guifg=DarkBlue
  set foldcolumn=2
  set foldclose=
  set foldmethod=indent
  set foldnestmax=10
  set foldlevel=0
  set fillchars=vert:\|,fold:\
  set foldminlines=1
 " Toggle fold state between closed and opened.
  "
  " If there is no fold at current line, just moves forward.
  " If it is present, reverse it's state.
  fu! ToggleFold()
     if foldlevel('.') == 0
         normal! l
     else
         if foldclosed('.') < 0
             . foldclose
         else
             . foldopen
         endif
     endif
     echo
  endf

" Map this function to Space key.
  noremap <space> :call ToggleFold()<CR>
like image 43
Kyle Walsh Avatar answered Sep 26 '22 22:09

Kyle Walsh