1

我的 .vimrc 中有这行:

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

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

当文件中有尾随空格时,它可以正常工作(它会删除空格并保存文件),但是当没有尾随空格时,它不会保存文件,它只是说Pattern not found: \s+$

注意:我找到了这个,但我很好奇我问的方式。

4

1 回答 1

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
于 2012-06-21T21:16:06.443 回答