Vim has a couple of functions to help with this, namely winsaveview()
and winrestview()
. Here's an example of how you might use them:
fun! Stayput(cmd)
let v = winsaveview()
exec a:cmd
call winrestview(v)
endfun
nno <leader>s mmA # => <Esc>`m:silent call Stayput('%!seeing_is_believing ...')<CR>
So save your view, do some stuff, then restore your view, including cursor and scroll positions, to exactly
how it was before. I've wrapped it up in a function for convenience.
A few other notes:
- consider using
:nnoremap
rather than :nmap
;
- I've used a mark in the mapping to return the cursor to where it
was before the comment was appended;
- I've used
:silent
to prevent editing messages appearing
in the last line of the screen; and
- I'm not sure what the
.
in %.!
is meant to be doing.
More info. at :help winsaveview()
.
seeing_is_believing
is, but you could always mark your position at the beginning of the command withma
and then return to that position with `a.