Sathish
Sathish

Reputation: 21090

How can i fold both code and comments in Vim?

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?

Upvotes: 0

Views: 1100

Answers (4)

Tinmarino
Tinmarino

Reputation: 4051

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

Upvotes: 0

emallove
emallove

Reputation: 1567

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

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172738

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.)

Upvotes: 2

qqx
qqx

Reputation: 19475

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.

Upvotes: 0

Related Questions