Question

Vim can fold Ruby code, but not comments.

After adding this in .vimrc to change foldmethod to comments, i can no longer fold code.

autocmd FileType ruby,eruby
  \ set foldmethod=expr |
  \ set foldexpr=getline(v:lnum)=~'^\\s*#'

How can i configure Vim to fold both comments and code?

Was it helpful?

Solution

In my recent Vim 7.3.823 snapshot, the $VIMRUNTIME/syntax/ruby.vim (version 2009 Dec 2) has both folding for Ruby constructs and comment blocks.

Just put

:let g:ruby_fold = 1

into ~/.vimrc. (And make sure you don't have a variable named ruby_no_comment_fold.)

OTHER TIPS

You could use foldmethod=marker and add {{{ / }}} markers (or other markers of your choosing) to indicate where folds begin and end.

You could also modify the file which defines ruby syntax highlighting to adjust what it considers as eligible for folding with foldmethod=syntax.

A third option would be to develop a more complex routine for use with foldmethod=expr. For example, I use the vim functions defined here to define how ruby code should be folded. It automatically defines folds for modules, classes and methods along with any comment lines that immediately precede those; and it supports the standard fold markers for folding other sections. It gets used with foldexpr=ruby#MethodFold(v:lnum).

Further information on how fold expressions should behave can be found by doing :help fold-expr. There's also a nice vimcast about that.

Setting foldmethod to indent will fold lines based on indent level, regardless of whether the line is a comment or code.

:set foldmethod=indent
:help fold-indent

I think you are looking for

set foldignore=#

If you want to fold block comments (like /* .... */ in multiple line), watch my other post in vi.stackechange

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top