【问题标题】:Map :w to Escape in insert mode and normal mode在插入模式和正常模式下将 :w 映射到 Escape
【发布时间】:2013-07-03 19:19:27
【问题描述】:

为了节省在 Vim 中的时间,我想出了一个主意。在正常模式和插入模式下将 :w 键绑定映射到 Esc。但是,它仅在插入模式下有效,而在正常模式下,当我打开一个新文件时,事情变得一团糟。这是我在 .vimrc 中添加的:

:inoremap <Esc> <Esc>:w<CR>
:nnoremap <Esc> :w<CR>

正如我所说的单独的第一个命令,工作正常。但是添加第二个命令,特别是当我打开一个新文件时,键会被弄乱。例如,虽然我在 .vimrc 中明确添加了:

map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>

在普通模式下添加第二条命令,按上下左右键进入插入模式,加A B C D。

你能帮我实现我的想法吗?

【问题讨论】:

    标签: vim map key remap


    【解决方案1】:

    你也可以使用

    autocmd InsertLeave * write

    【讨论】:

      【解决方案2】:

      Vim FAQ 10.9 may be useful的信息:

      10.9. When I use my arrow keys, Vim changes modes, inserts weird characters
           in my document but doesn't move the cursor properly. What's going on?
      
      There are a couple of things that could be going on: either you are using
      Vim over a slow connection or Vim doesn't understand the key sequence that
      your keyboard is generating.
      
      If you are working over a slow connection (such as a 2400 bps modem), you
      can try to set the 'timeout' or 'ttimeout' option. These options, combined
      with the 'timeoutlen' and 'ttimeoutlen' options, may fix the problem.
      
      The preceding procedure will not work correctly if your terminal sends key
      codes that Vim does not understand. In this situation, your best option is
      to map your key sequence to a matching cursor movement command and save
      these mappings in a file. You can then ":source" the file whenever you work
      from that terminal.
      
      For more information, read 
      
          |'timeout'|
          |'ttimeout'|
          |'timeoutlen'|
          |'ttimeoutlen'|
          |:map|
          |vt100-cursor-keys|
      

      来自:h vt100-cursor-keys

      Other terminals (e.g., vt100 and xterm) have cursor keys that send <Esc>OA,
      <Esc>OB, etc. ...
      

      所以可能是您的nnoremap 导致箭头键序列上的Esc 保存文件,其余字符被单独解释,因此A 正在进入插入模式。

      您可以考虑使用选项'autowriteall',或使用不同的映射来保存您的文件;这些定义在$VIMRUNTIME\mswin.vim:

      " Use CTRL-S for saving, also in Insert mode
      noremap <C-S>       :update<CR>
      vnoremap <C-S>      <C-C>:update<CR>
      inoremap <C-S>      <C-O>:update<CR>
      

      :update 命令类似于:w,但仅在文件已被修改时才写入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 2017-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多