Removing trailing white space only for edited lines

廉价感情. 提交于 2019-12-05 01:21:19
George Hilliard

mMontu's answer has the right idea, but it doesn't handle an edge case. Namely, if I move the cursor down, then back up, all in edit mode, it doesn't pick up the changes to those lines. If we would like to handle this, then we need to store the top and bottom lines visited by the user. Here is some more robust code, with everything grouped into functions:

fun! <SID>SetupTrailingWhitespaces()
    let curline = line('.')
    let b:insert_top = curline
    let b:insert_bottom = curline
endfun

fun! <SID>UpdateTrailingWhitespace()
    let curline = line('.')
    if b:insert_top > curline
        let b:insert_top = curline
    elseif b:insert_bottom < curline
        let b:insert_bottom = curline
    endif
endfun

fun! <SID>StripTrailingWhitespaces()
    let original_cursor = getpos('.')
    exe b:insert_top ',' b:insert_bottom 's/\s\+$//e'
    call setpos('.', original_cursor)
endfun

Now we just invoke these functions at the right time:

autocmd InsertEnter * :call <SID>SetupTrailingWhitespaces()
autocmd InsertLeave * :call <SID>StripTrailingWhitespaces()
autocmd CursorMovedI * :call <SID>UpdateTrailingWhitespace()

Alternatively, I've written a plugin that provides this updated functionality, with several additional features like stripping in normal mode as well.

mMontu

One possibility would be to use autocmd InsertLeave to strip white spaces from current line every time you leave insert mode:

autocmd InsertLeave *.[ch] :call <SID>StripTrailingWhitespaces()

, and change substitute command of StripTrailingWhitespaces() function changed to

s/\s\+$//e

It will work if all lines that you include doesn't end in white spaces, only the last one. It will possible change lines that you didn't modified, if you enter and exit insert mode (i followed by ESC).

This solution can be changed to work if you include lines that does end in white space (pasted lines from legacy code, for example):

autocmd InsertEnter *.[ch] :let b:insert_start = line('.')
autocmd InsertLeave *.[ch] :call <SID>StripTrailingWhitespaces()

fun! StripTrailingWhitespaces()
    let original_cursor = getpos('.')
    exe b:insert_start . ',.s/\s\+$//e'
    call setpos('.', original_cursor)
endfun     

If the replacement on lines due to enter and exit insert mode (i followed by ESC) is a problem then the solution could save b:changedtick-variable when entering insert mode and check it when leaving insert mode to detect changes.

I write a plugin named 'vim-scavenger' to clean up multiple blank lines and trailing spaces.

Just add the following config in your .vimrc:

let g:scavenger_auto_clean_up_on_write = 1

For more detail, you can come to that Github repo to learn more. Feel free to give me advice to improve the plugin.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!