删除仅用于编辑行的尾部空白 [英] Removing trailing white space only for edited lines

查看:61
本文介绍了删除仅用于编辑行的尾部空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.vimrc中添加了以下功能,用于在保存前删除尾随空格

I had added the following function in my .vimrc for removing trailing white spaces just before saving

fun! <SID>StripTrailingWhitespaces()                                            
    let l = line(".")                                                           
    let c = col(".")                                                            
    %s/\s\+$//e                                                                 
    call cursor(l, c)                                                           
endfun                                                                          

autocmd BufWritePre *.h :call <SID>StripTrailingWhitespaces()
autocmd BufWritePre *.cpp :call <SID>StripTrailingWhitespaces()
autocmd BufWritePre *.c :call <SID>StripTrailingWhitespaces()

效果很好。但是,在某些情况下,我只想从已编辑的行中删除结尾的空格。这是为了确保我的 diff 输出看起来是合理的,因为对于某些旧版代码文件,几乎所有行都以空格结尾,并且我不想给我的代码审阅者增加不必要的差异。

It works really well. However, in certain cases I would like to remove trailing white spaces only from lines that I have edited. This is to ensure that my diff output looks sane as for certain legacy code files almost all lines have trailing back spaces and I do not want to burden my code reviewer with unnecessary diff.

diff -b 现在不是解决方案,因为它也忽略行中任何地方的空白,有时该更改非常重要,足以包含在 diff 输出中。

diff -b is not a solution right now since it also ignores white spaces from anywhere in a line and sometimes that change is important enough to be include in the diff output.

所以我的问题是-是否可以

So my question is - is it possible to strip trailing white spaces from only the lines that have been edited in this session in a file in vim?

推荐答案

mMontu的答案是正确的主意,但无法处理极端情况。也就是说,如果我先将光标向下移动,然后又向上移动,则全部处于编辑模式,则不会接受对这些行所做的更改。如果我们要处理此问题,则需要存储用户访问过的顶行和底行。这是一些更健壮的代码,所有内容都归类为函数:

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.

这篇关于删除仅用于编辑行的尾部空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆