How can you automatically remove trailing whitespace in vim

后端 未结 13 2070
星月不相逢
星月不相逢 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:08

    Compilation of above plus saving cursor position:

    fun! <SID>StripTrailingWhitespaces()
        let l = line(".")
        let c = col(".")
        keepp %s/\s\+$//e
        call cursor(l, c)
    endfun
    
    autocmd FileType c,cpp,java,php,ruby,python autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()
    

    If you want to apply this on save to any file, leave out the second autocmd and use a wildcard *:

    autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
    
    0 讨论(0)
  • 2020-12-02 04:08

    Copied and pasted from http://blog.kamil.dworakowski.name/2009/09/unobtrusive-highlighting-of-trailing.html (the link no longer works, but the bit you need is below)

    "This has the advantage of not highlighting each space you type at the end of the line, only when you open a file or leave insert mode. Very neat."

    highlight ExtraWhitespace ctermbg=red guibg=red
    au ColorScheme * highlight ExtraWhitespace guibg=red
    au BufEnter * match ExtraWhitespace /\s\+$/
    au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
    au InsertLeave * match ExtraWhiteSpace /\s\+$/
    
    0 讨论(0)
  • 2020-12-02 04:09

    If you trim whitespace, you should only do it on files that are already clean. "When in Rome...". This is good etiquette when working on codebases where spurious changes are unwelcome.

    This function detects trailing whitespace and turns on trimming only if it was already clean.

    The credit for this idea goes to a gem of a comment here: https://github.com/atom/whitespace/issues/10 (longest bug ticket comment stream ever)

    autocmd BufNewFile,BufRead *.test call KarlDetectWhitespace()
    
    fun! KarlDetectWhitespace()
    python << endpython
    import vim
    nr_unclean = 0
    for line in vim.current.buffer:
        if line.rstrip() != line:
            nr_unclean += 1
    
    print "Unclean Lines: %d" % nr_unclean
    print "Name: %s" % vim.current.buffer.name
    cmd = "autocmd BufWritePre <buffer> call KarlStripTrailingWhitespace()"
    if nr_unclean == 0:
        print "Enabling Whitespace Trimming on Save"
        vim.command(cmd)
    else:
        print "Whitespace Trimming Disabled"
    endpython
    endfun
    
    fun! KarlStripTrailingWhitespace()
        let l = line(".")
        let c = col(".")
        %s/\s\+$//e
        call cursor(l, c)
    endfun
    
    0 讨论(0)
  • 2020-12-02 04:10

    For people who want to run it for specific file types (FileTypes are not always reliable):

    autocmd BufWritePre *.c,*.cpp,*.cc,*.h,*.hpp,*.py,*.m,*.mm :%s/\s\+$//e
    

    Or with vim7:

    autocmd BufWritePre *.{c,cpp,cc,h,hpp,py,m,mm} :%s/\s\+$//e
    
    0 讨论(0)
  • 2020-12-02 04:14

    autocmd BufWritePre * :%s/\s\+$//<CR>:let @/=''<CR>

    0 讨论(0)
  • 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\+\%#\@<!$/
    autocmd InsertLeave * match ExtraWhitespace /\s\+$/
    autocmd BufWinLeave * call clearmatches()
    

    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()
    
    0 讨论(0)
提交回复
热议问题