2013-07-05 33 views
1

为了节省Vim的时间,我提出了一个想法。要在正常模式和插入模式下映射:w键绑定到Esc。然而,它只能在插入模式下工作,而在正常模式下,当我打开一个新文件时,事情变得混乱。这是我在.vimrc中添加的内容:地图:w在插入模式和正常模式下退出

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

正如我刚才说的第一个命令,工作正常。但是,当我打开一个新文件时,添加第二个命令,特别是ESPECIALLY会导致密钥混乱。举例来说,虽然我已经明确地在.vimrc里加入:

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

通过添加第二个命令为正常模式,按上下左右键导致插入模式进入,并添加ABC D.

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

回答

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

此外,您还可以使用

autocmd InsertLeave * write

相关问题