Map:w以插入模式和普通模式退出

时间:2013-07-05 16:13:00

标签: vim map key remap

为了节省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。

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

2 个答案:

答案 0 :(得分:3)

有关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,但仅在文件被修改时才写入。

答案 1 :(得分:2)

此外,您可以使用

autocmd InsertLeave * write