Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can one make vim's fold function - when using foldmethod=syntax - also hide one newline after each function, if-else and struct (using Go).

share|improve this question
up vote 4 down vote accepted

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


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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.