Skip to content

Add "auto remove trailing whitespace" function #30

@chusiang

Description

@chusiang

Thanks @aeglos for share this magic.

" Remove trailing whitespace when writing a buffer, but not for diff files.
" From: Vigil <[email protected]>
function RemoveTrailingWhitespace()
    if &ft != "diff"
       let b:curcol = col(".")
        let b:curline = line(".")
        silent! %s/\s\+$//
        silent! %s/\(\s*\n\)\+\%$//
        call cursor(b:curline, b:curcol)
    endif
endfunction

"autocmd BufWritePre * call RemoveTrailingWhitespace()
map <localleader>r : call RemoveTrailingWhitespace()<cr>


" show trailing white spaces
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions