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.
Asked
Active
Viewed 489 times
6

dc-
- 1,878
- 1
- 15
- 15
3 Answers
7
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>'

Ingo Karkat
- 167,457
- 16
- 250
- 324
2
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.

Walter
- 7,809
- 1
- 30
- 30
-
Combining this into `:au CursorMoved * :normal zv` should do the trick. – MaienM May 04 '12 at 23:19
-
@MailenM: That will open any fold you move over, even when moving with `j` / `k` -- I doubt this is useful. – Ingo Karkat May 05 '12 at 19:24
0
Define a new command mapping. In this example, I chose \gz:
:nmap \gz gg<Bar>zO

Dan Applegate
- 346
- 2
- 6