How can you automatically remove trailing whitespace in vim

后端 未结 13 2132
星月不相逢
星月不相逢 2020-12-02 03:39

I am getting \'trailing whitespace\' errors trying to commit some files in git.

I want to remove these trailing whitespace characters automatically right before I sa

13条回答
  •  难免孤独
    2020-12-02 04:19

    I both highlight existing trailing whitespace and also strip trailing whitespace.

    I configure my editor (vim) to show white space at the end, e.g.

    enter image description here

    with this at the bottom of my .vimrc:

    highlight ExtraWhitespace ctermbg=red guibg=red
    match ExtraWhitespace /\s\+$/
    autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
    autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@

    and I 'auto-strip' it from files when saving them, in my case *.rb for ruby files, again in my ~/.vimrc

    function! TrimWhiteSpace()
        %s/\s\+$//e
    endfunction
    autocmd BufWritePre     *.rb :call TrimWhiteSpace()
    

提交回复
热议问题