Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically expand fold when going to a line number

Is there a way to unfold code when going to a line number? For instance I type :35 where line 35 is folded, then I have to unfold that section manually to actually get to that line. I would like to type :35and have that code then unfolded automatically and my cursor put on line 35 without any further key presses.

like image 217
dc- Avatar asked May 04 '12 15:05

dc-


2 Answers

If you use the 35G command instead of :35, you can achieve this with the following mapping:

"[count]G       Also open fold under cursor when supplying [count] (i.e.
"               jumping to a particular line, not the end of the
"               buffer). Use [count]|gg| if you don't want this.
nnoremap <expr> G (v:count ? 'Gzv' : 'G')

For :35 itself, this would be hard to achieve. You would have to intercept the <CR> via a :cmap <expr>, check the typed command via getcmdtype() and getcmdline(), and, if it's a number, manipulate the command, i.e. append normal! zv to it; like this:

cmap <expr> <CR> getcmdtype() == ':' && getcmdline() =~ '^\d\+$' ? 'normal! zv<CR>' : '<CR>'
like image 148
Ingo Karkat Avatar answered Sep 29 '22 04:09

Ingo Karkat


zv. From :help zv:

    View cursor line: Open just enough folds to make the line in
    which the cursor is located not folded.

While this command could probably be triggered automatically in some way, I have not come across it yet. Using the command as-is has served me well, though.

like image 33
Walter Avatar answered Sep 29 '22 04:09

Walter