8

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

4

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.

| improve this answer | |
0

I know it's old, but additionally to @ansgar-esztermann solution:

syntax clear  cBlock
syntax match  cBlock1line /.\s*{.\{-}}/ transparent contains=ALLBUT,cBadBlock,cCurlyError,@cParenGroup,cErrInParen,cErrInBracket,cBlockImp
syntax region cBlockImp  start=/{/  end=/};\?\(\n\n\)\?/ transparent fold contains=ALLBUT,cBadBlock,cCurlyError,@cParenGroup,cErrInParen,cErrInBracket

in ~/.vim/syntax/c.vim folds new line (for structs too)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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