vim: function whitespaces

This commit is contained in:
Michael Grote 2023-08-14 09:13:41 +02:00
parent 58c554a7e9
commit 9bb6fb8e49

27
.vimrc
View file

@ -234,15 +234,24 @@ nnoremap <Leader>z :set nonumber! norelativenumber!<CR>
: autocmd BufLeave,FocusLost,InsertEnter,WinLeave * if &nu | set nornu | endif
:augroup END
" entferne trailing whitespaces beim speichern
autocmd BufWritePre * :%s/\s\+$//e
" entferne multiple consecutive empty lines
if executable('cat')
autocmd BufWritePre *.md silent %!cat -s
endif
" remove multiple whitespaces between words
" https://stackoverflow.com/questions/5150870/efficient-way-to-remove-multiple-spaces-between-two-words-in-Vim
autocmd BufWritePre *.md :%s/\(\w\)\s\+\(\w\)/\1 \2/g
function! Whitespaces()
" Remove trailing whitespace
%s/\s\+$//e
" Replace consecutive spaces between words with a single space (only for .md files)
if &filetype == "markdown"
%s/\(\w\)\s\+\(\w\)/\1 \2/g
endif
" Run silent command to remove multiple consecutive empty lines (only for .md files)
if &filetype == "markdown"
silent %!cat -s
endif
endfunction
" Map F9 to execute the custom function
nnoremap <F9> :call Whitespaces()<CR>
" Exit Vim if NERDTree is the only window remaining in the only tab.