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.

  • 2
    Oddly enough, I tried zMzv today and it kept the correct cursor position... curious.
    – D. Ben Knoble
    Commented Nov 7, 2017 at 0:06
  • I should be more clear about this, I mapped to a key so that it will contain <CR> at the end and reset cursor to next line. Commented Nov 7, 2017 at 0:13
  • Why do you need the <CR> though? I.e., why doesn’t for example nnoremap <leader>zo zMzv do the trick?
    – D. Ben Knoble
    Commented Nov 7, 2017 at 0:17
  • Sorry about the question, you're right. But screen will blink because close and open fold task - the output. mark seems sovled it, but Idk why. Commented Nov 7, 2017 at 0:28

1 Answer 1


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 ;)

  • 1
    I 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.
    – Mass
    Commented 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.