Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

1 Answer 1

up vote 3 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

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.

share|improve this answer

Your Answer

 
discard

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.