I have some Vim functions that make changes to the document format. When I call this function, I currently use something like the following to save and restore my cursor positio
You can save a mark for the first on-screen line that is displayed in the window and restore that as well. An example that executes a g?
command on the whole buffer and restores both positions:
:noremap <F11> mkHmlggg?G`lzt`k
Walking through the command:
mk
: set mark k
for the current positionH
: go to the first on-screen lineml
: set mark l
for the this positionggg?G
: execute the command: jump to mark
l`zt
: set this line the first on-screen line: jump to mark
k`you can use getline() to save the current buffer line and winline() to save the current window line.
So it would go something like this:
There might be a few special cases you will have to take care of such as if the position is near the end or beginning of the file or if the file is smaller then the window size.
let l:winview = winsaveview()
" do stuff
call winrestview(l:winview)
This should pretty much do exactly what you want it to do, possibly excepting the line count changing above the cursor (I suspect that deleted lines above the cursor would have the effect of moving the cursor down).
Just :h getpos()
let save_cursor = getpos(".")
" MoveTheCursorAround
call setpos('.', save_cursor)
There is a plugin but I use a single function like this:
if !exists('*Preserve')
function! Preserve(command)
try
" Preparation: save last search, and cursor position.
let l:win_view = winsaveview()
let l:old_query = getreg('/')
silent! execute 'keepjumps ' . a:command
finally
" Clean up: restore previous search history, and cursor position
call winrestview(l:win_view)
call setreg('/', l:old_query)
endtry
endfunction
endif
then I call it to clean trailing spaces
fun! CleanExtraSpaces()
call Preserve(':%s/\s\+$//ge')
endfun
com! Cls :call CleanExtraSpaces()
au! BufwritePre * :call CleanExtraSpaces()
del blank lines
fun! DelBlankLines()
call Preserve(':%s/^\n\{2,}/\r/ge')
endfun
command! -nargs=0 DelBlank :call DelBlankLines()
and change Header (Last Modified) information
fun! ChangeHeader()
call Preserve(':1,5s/Last Change: \zs.*/\=strftime("%c")/e')
endfun
command! -nargs=0 CH :call ChangeHeader()
au BufWritePost * :call ChangeHeader()