2012-06-21 50 views
1

我在我的.vimrc部份線路:關於鍵綁定刪除尾隨空格和保存

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

刪除尾隨空格並保存在同一時間的文件。

當文件中有尾隨空格時,它工作正常(它刪除空格並保存文件),但是當沒有尾隨空格時它不保存文件,它只是說模式未找到: \ s + $

注:我發現this,但我很好奇我問的方式。

+0

我認爲這也是在您沒有寫權限的文件有問題 - 我只是做手寫後寫 – epsilonhalbe

回答

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