Question

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.

Was it helpful?

Solution

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>'

OTHER TIPS

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.

Define a new command mapping. In this example, I chose \gz:

:nmap \gz gg<Bar>zO
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top