Delete all spaces and tabs at the end of my lines

In vim:

:%s/\s\+$//

Explanation:

  • : command
  • % apply to entire file
  • s search and replace
  • /\s\+$/ regex for one or more whitespace characters followed by the end of a line
  • // replacement value of an empty string

I use this function :

func! DeleteTrailingWS()
  exe "normal mz"
  %s/\s\+$//ge
  exe "normal `z"
endfunc

Leader,w to remove trailing white spaces

noremap <leader>w :call DeleteTrailingWS()<CR>

Remove trailing white spaces when saving a python file:

autocmd BufWrite *.py :call DeleteTrailingWS()