Advertisement
voyeg3r

vim-preserve.vim

Mar 26th, 2019
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 1.19 KB | None | 0 0
  1. " My solution to reident, remove duplicated blank lines and things like that
  2. " envoles the function Preserve(), it allows me doing a lot of stuff withou moving
  3. " my cursor or messing up with my jumps and search registers
  4.  
  5. " Utility function that save last search and cursor position
  6. " http://technotales.wordpress.com/2010/03/31/preserve-a-vim-function-that-keeps-your-state/
  7. " video from vimcasts.org: http://vimcasts.org/episodes/tidying-whitespace
  8. " using 'execute' command doesn't overwrite the last search pattern, so I
  9. " don't need to store and restore it.
  10. " preserve function
  11. if !exists('*Preserve')
  12.     function! Preserve(command) range
  13.         try
  14.             let l:win_view = winsaveview()
  15.              silent! keepjumps keeppatterns execute a:command
  16.         finally
  17.             call winrestview(l:win_view)
  18.         endtry
  19.     endfunction
  20. endif
  21.  
  22. " remove consecutive blank lines
  23. " see Preserve function definition
  24. fun! DelBlankLines() range
  25.     if !&binary && &filetype != 'diff'
  26.         call Preserve(':%s/\s\+$//e')
  27.         call Preserve(':%s/^\n\{2,}/\r/ge')
  28.     endif
  29. endfun
  30.  
  31. command! -nargs=0 DelBlank :call DelBlankLines()
  32. nnoremap <Leader>d :call DelBlankLines()<cr>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement