English 中文(简体)
How can I use Vim syntax files to collapse single line comments into one region?
原标题:

I m putting together a syntax for editing Java Manifest files (at github, if anyone s interested). I m trying to collapse multiple single-line-comments (which I m matching at the moment with syntax match manifestComment "#.*"). However, if I try to use a syntax region, then the entire file is marked and the entire thing collapses.

What I m trying to achieve is this:

# A comment
# Another comment
# A third comment
Manifest-Version: 1

and have it collapse down into:

+--  3 lines: # A comment ----
Manifest-Version: 1 

The problem is that there s no distinct end character, and the fold syntax doesn t help; so I can t do syntax region commentBlock start="^#" end="^[^#]". Roughly, the syntax region should start from the first hash character, and then continue down the lines until it finds a line which doesn t begin with a hash.

最佳回答
:set foldmethod=expr
:set foldexpr=getline(v:lnum)[0]=="#"

For information, :h fold-expr.

问题回答

How about syntax region commentBlock start="^#" end="^#@!"?

The @! is like (?!pattern) in Perl, i.e. it matches with zero-width if the preceding atom -- # in this case -- does not match at the current position.





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签