2013-03-16 19 views
2

我在vim中設置了一個預緩衝區寫入掛鉤,它在將緩衝區的內容保存到文件之前進行一些小的調整。停止vim函數中的替換漏入所有替換的歷史記錄

if !exists("autocommands_loaded") 
    let autocommands_loaded = 1 
    autocmd BufWritePre *.php call TidyUpFormatting() 
endif 

func! TidyUpFormatting() 
    let save_cursor = getpos('.') 
    %s/\s\+$//ge 
    %s/\($\n\s*\)\+\%$//ge 
    %s/var_dump /var_dump/ge 
    %s/){/) {/ge 
    %s/(/(/ge 
    %s/if(/if (/ge 
    %s/while(/while (/ge 
    call setpos('.', save_cursor) 
endfunction 

這是在我的ftplugin/php.vim文件中。 我注意到,儘管TidyUpFormatting中的這些替換顯示在所有替換的歷史記錄中 - 例如,如果我通過手動完成的替換列表向上滾動,那麼它們就是。

有沒有一個標誌我可以使用,或者確實有更好的方法來做到這一點,以便這些替換不會「泄漏」?

回答

3

搜索模式的功能裏面確實不污染的搜索歷史(一次爲整個功能,不是每個:s)。您可以通過在該功能的末尾添加以下內容來解決此問題:

:call histdel('search', -1) 
+0

這似乎有幫助 - 謝謝! – kguest 2013-03-18 16:39:06

0

我需要前綴與「沉默」命令替換命令,改變TidyUpFormatting功能:從:substitution

func! TidyUpFormatting() 
    let save_cursor = getpos('.') 
    silent! %s/\s\+$//ge 
    silent! %s/\($\n\s*\)\+\%$//ge 
    silent! %s/var_dump /var_dump/ge 
    silent! %s/){/) {/ge 
    silent! %s/(/(/ge 
    silent! %s/if(/if (/ge 
    silent! %s/while(/while (/ge 
    call setpos('.', save_cursor) 
endfunction