关于删除尾随空格和保存的键绑定

时间:2012-06-21 21:06:42

标签: vim

我的.vimrc中有这一行:

map gu :%s/\s\+$//<enter> :w<enter>

删除尾随空格并同时保存文件。

当文件中有尾随空格时,它工作正常(它删除空格并保存文件),但是当没有尾随空格时它不保存文件,它只是说模式未找到:\ s + $

注意:我找到了this,但我很好奇我的问题。

1 个答案:

答案 0 :(得分:7)

在搜索标记中添加e以禁止显示错误消息:

map gu :%s/\s\+$//e<enter> :w<enter>

来自:help substitute

[e]     When the search pattern fails, do not issue an error message and, in 
        particular, continue in maps as if no error occurred.  This is most 
        useful to prevent the "No match" error from breaking a mapping.  Vim 
        does not suppress the following error messages, however:  
            Regular expressions can't be delimited by letters
            \ should be followed by /, ? or &
            No previous substitute regular expression
            Trailing characters
            Interrupted