2016-03-22 43 views
0

我在.gvimrc下面的代碼,以防止我從保存文件名爲1偶然的命令時,我不小心撞到:w1代替:w!GVIM防止

autocmd BufWritePre [1]* throw 'Forbidden file name: ' . expand('<afile>') 

有沒有一種方法可以讓我做類似的事情防止意外進入:e1(刷新緩衝區)?我無法找到一個BufNewFilePre

回答

0

BufNew應該工作:

只需創建一個新的緩衝作用。也可以在緩衝區重命名後使用。當緩衝區被添加到緩衝區列表時BufAdd也會被觸發。

autocmd BufNew [1]* throw 'Forbidden file name: ' . expand('<afile>') 

這給了我一個錯誤,離開當前緩衝區完好,當我做:e1

0

爲什麼不自動糾正你的胖指令?基於http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev

" command to define abbreviation that only affects first typed word in the 
" command line 
command! -nargs=+ CommandCabbr call CommandCabbr(<f-args>) 

" helper function to create command-line abbreviations 
function! CommandCabbr(abbreviation, expansion) 
    if exists('*getcmdpos') 
    if exists('*getcmdtype') 
     " only expand when on COMMAND line and at first position 
     execute "cabbrev ".a:abbreviation. 
      \' <c-r>=getcmdpos() == 1 && getcmdtype() == ":" ? "'. 
      \escape(a:expansion,'"\').'" : "'. 
      \escape(a:abbreviation,'"\').'"<CR>' 
    else 
     " can't test command line, but only expand at first position 
     execute "cabbrev ".a:abbreviation. 
      \' <c-r>=getcmdpos() == 1 ? "'. 
      \escape(a:expansion,'"\').'" : "'. 
      \escape(a:abbreviation,'"\').'"<CR>' 
    endif 
    else 
    " fall back to always expanding :-(
    execute "cabbrev ".a:abbreviation." ".a:expansion 
    endif 
endfunction 

CommandCabbr w1  w! 
CommandCabbr [email protected] w! 
CommandCabbr [email protected]! w! 
CommandCabbr w!~ w! 
CommandCabbr w~! w! 
CommandCabbr [email protected]  w! 
CommandCabbr w~  w! 
" etc.