Created
June 22, 2012 15:29
-
-
Save docwhat/2973488 to your computer and use it in GitHub Desktop.
Wrapper function to save your cursor and window positions and your last search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Re-indents buffer. | |
nmap <silent> <Leader>g :call Preserve("normal gg=G")<CR> | |
" Removes all trailing whitespace in buffer. | |
nmap <silent> <Leader><space> :call Preserve("%s/\\s\\+$//e")<CR> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" A wrapper function to restore the cursor position, window position, | |
" and last search after running a command. | |
function! Preserve(command) | |
" Save the last search | |
let last_search=@/ | |
" Save the current cursor position | |
let save_cursor = getpos(".") | |
" Save the window position | |
normal H | |
let save_window = getpos(".") | |
call setpos('.', save_cursor) | |
" Do the business: | |
execute a:command | |
" Restore the last_search | |
let @/=last_search | |
" Restore the window position | |
call setpos('.', save_window) | |
normal zt | |
" Restore the cursor position | |
call setpos('.', save_cursor) | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is from a blog post: https://docwhat.org/vim-preserve-your-cursor-and-window-state/