我在成功将 key 重新映射到 <Esc>
时遇到一些问题。我已经尝试过使用多个字符,所以假设我想重新映射 1
至<Esc>
。在我的.vimrc
文件中,我添加了以下行:
noremap! 1 <Esc>
这对于退出插入模式效果很好,但是当我处于命令模式时,命令会被执行而不是退出。例如,在正常模式下,如果我输入:
/searchtext1
显示“searchtext”的搜索结果,而不是退出到正常模式而不进行搜索。对于以 :
开头的命令也是如此。有解决方法吗?我是否使用了错误的 map 功能?
我从终端使用 Vim,尽管在 MacVim GUI 上测试后,它也有这个问题。
最佳答案
根据文档,这是预期行为。它是 vi 兼容性的一部分。
如果你看:h i_esc | /*3 Go
您将看到以下段落。
*3 Go from Command-line mode to Normal mode by:
- Hitting <CR> or <NL>, which causes the entered command to be executed.
- Deleting the complete line (e.g., with CTRL-U) and giving a final <BS>.
- Hitting CTRL-C or <Esc>, which quits the command-line without executing
the command.
In the last case <Esc> may be the character defined with the 'wildchar'
option, in which case it will start command-line completion. You can
ignore that and type <Esc> again. {Vi: when hitting <Esc> the command-line
is executed. This is unexpected for most people; therefore it was changed
in Vim. But when the <Esc> is part of a mapping, the command-line is
executed. If you want the Vi behaviour also when typing <Esc>, use ":cmap
^V<Esc> ^V^M"}
以 {Vi: ... }
开头的部分正在谈论 vi 兼容性并解释您所看到的行为。
映射:noremap! 1 <c-u><bs>
会做你想做的事。也就是彻底删除当前行。这是列表中的第 2 条。
关于macos - 在命令模式下将按键重新映射到 <Esc> 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396516/