Automatically expand fold when going to a line number

folding, macvim, vim

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

Problem

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 `:35`and have that code then unfolded automatically and my cursor put on line 35 without any further key presses.

Original source