I would like to map a key so I can quick close all folds but keep unfolded on current cursor and keep cursor's position too.
Through :h fold
I can use zMzv
but it can't keep cursor's position.
1 Answer
You can set a mark at the beginning of your mapping and then jump there at the end. For example:
nnoremap <leader>zv :normal mazMzv`a<CR>
Postscript: This was posted when I was merely an apprentice Vim Ninja rather than the MVN (Master Vim Ninja) you see before you today. Of course, in the <ahem> normal course of things one would be wasteful to create such an nmap when nno <leader>zv mazMzv`a
is equivalent. Funny thing, though...OP says the less enlightened version was the only one to solve his problem. Personally I can't reproduce even the cursor moving part so I just take them at their word and this answer lives on; a signpost aside the road on which I journeyed to top of the mountain. B Layer - 6/14/2020 ;)
-
1I am curious if am I missing something? If I press
zx
,zv
,zMzv
, etc.. the folds change but the cursor does not move. So I don't understand why the mark is necessary.– MassCommented Nov 6, 2017 at 20:17 -
If you map
zx
to a key:nnoremap <space>zx :zx<CR>
for i.e, ` zx` will change cursor's position. Commented Nov 7, 2017 at 0:12
zMzv
today and it kept the correct cursor position... curious.<CR>
at the end and reset cursor to next line.<CR>
though? I.e., why doesn’t for examplennoremap <leader>zo zMzv
do the trick?