Gvim防止意外命令

时间:2016-03-22 19:43:18

标签: vim

我在.gvimrc中有以下代码,以防止我在意外点击1而不是:w1

时保存名为:w!的文件
autocmd BufWritePre [1]* throw 'Forbidden file name: ' . expand('<afile>')

我是否有办法防止意外输入:e1(刷新缓冲区)?我找不到BufNewFilePre

2 个答案:

答案 0 :(得分:0)

BufNew应该有效:

  

创建新缓冲区后。也在重命名缓冲区后使用。当缓冲区添加到缓冲区列表时,BufAdd也将被触发。

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

当我执行:e1时,这会给我一个错误并保留当前缓冲区。

答案 1 :(得分: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 w!@    w!
CommandCabbr w@!    w!
CommandCabbr w!~    w!
CommandCabbr w~!    w!
CommandCabbr w@     w!
CommandCabbr w~     w!
" etc.