making vim fold also hide newline after function

Here's how to do it for C:

  • Take the original syntax file (under Unix, typically found in /usr/share/vim) and copy it to (again, under Unix) ~/.vim/syntax.
  • Change the "end" pattern in the cBlock region definition:
syntax region cBlock    start="{" end="}" transparent fold

becomes

syntax region cBlock    start="{" end="}\(\n\n\)\?" transparent fold

Here, the first \n matches the newline character immediately following the closing brace, the second one the empty line. This will not work if } is followed by e.g. a comment: the block will still fold, but the following newline will not.

I do not have a syntax file for Go here (as far as I can tell), but it should work in a similar way.

Note: the more flexible way would be to create a new file at ~/.vim/after/syntax and just change the region definition, but I have been unsuccessful here. Merely copying the region definition does not work.

Tags:

Vim