" https://vi.stackexchange.com/questions/454/whats-the-simplest-way-to-strip-trailing-whitespace-from-all-lines-in-a-file function! Preserve(command) " Preparation: save window state let l:saved_winview = winsaveview() " Run the command: execute a:command " Clean up: restore previous window position call winrestview(l:saved_winview) endfunction nnoremap ws :call Preserve("%s/\\s\\+$//e") xnoremap ws :call Preserve("%s/\\s\\+$//e")