From 9bb6fb8e49974547bac005cf616652174f7e5842 Mon Sep 17 00:00:00 2001 From: mg Date: Mon, 14 Aug 2023 09:13:41 +0200 Subject: [PATCH] vim: function whitespaces --- .vimrc | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/.vimrc b/.vimrc index 7ee678d..c7dba3e 100644 --- a/.vimrc +++ b/.vimrc @@ -234,15 +234,24 @@ nnoremap z :set nonumber! norelativenumber! : 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 :call Whitespaces() + " Exit Vim if NERDTree is the only window remaining in the only tab.