Browse Source

Add autocmd to remove trailing whitespaces on BufWritePre

Andrea Miglietta 7 years ago
parent
commit
d8f65da2fd
1 changed files with 14 additions and 5 deletions
  1. 14 5
      .vimrc

+ 14 - 5
.vimrc

@@ -17,7 +17,7 @@ set tabstop=4
 " Incremental and highlight search
 set incsearch
 set hlsearch
- 
+
 " Allow buffers to be hidden
 set hidden
 
@@ -31,7 +31,7 @@ set pastetoggle=<F12>
 set showmatch
 
 " Always show status bar
-set laststatus=2 
+set laststatus=2
 set statusline=%<\ %{expand('%:h')}/%t%m%r%=%-14.(%l,%c%V%)\ %p%%
 "set statusline=%<\ %F%m%r%=%-14.(%l,%c%V%)\ %p%%
 
@@ -53,9 +53,9 @@ endif
 
 " Jump to the last position when reopening a file
 if has("autocmd")
-    au BufReadPost * 
-    \ if line("'\"") > 1 && line("'\"") <= line("$") | 
-    \    exe "normal! g'\"" | 
+    au BufReadPost *
+    \ if line("'\"") > 1 && line("'\"") <= line("$") |
+    \    exe "normal! g'\"" |
     \ endif
 endif
 
@@ -81,3 +81,12 @@ augroup Shebang
     autocmd BufNewFile *.py 0put =\"#!/usr/bin/python\"|$
 augroup END
 
+" Removes trailing spaces
+function! TrimWhiteSpaces()
+    if search('\s\+$','bn') > 0
+        %s/\s\+$//
+    endif
+endfunction
+
+autocmd BufWritePre * call TrimWhiteSpaces()
+