Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim folding without end markers

Tags:

vim

I'd like to fold text between beginning markers like:

//// Block 1

... some code 1 ...

//// Block 2

... some code 2 ...

where //// would be used as the beginning marker and folding //// Block 1 would fold to the line before //// Block 2.

Is this something that's possible?

It doesn't like it if I set foldmarker without an ending parameter and it folds too much if I use //// as both the beginning and ending markers.

I could manually create folds with zf, but those are file dependent and break if you change it.

like image 395
ggeise Avatar asked Oct 19 '22 19:10

ggeise


1 Answers

You can write your own fold expression like this:

function! BlockFolds()
   let thisline = getline(v:lnum)
   if match(thisline, '^\/\/\/\/ Block') >= 0
      return ">1"
   else
      return "="
   endif
endfunction

setlocal foldmethod=expr
setlocal foldexpr=BlockFolds()

If you source it in vim you will get the desired effect:

demo

Obviously you can source it in your .vimrc or based on file extension. You can understand how custom fold expressions work here: http://vimcasts.org/episodes/writing-a-custom-fold-expression/

like image 135
enrico.bacis Avatar answered Oct 21 '22 22:10

enrico.bacis