vim - 为什么按 escape 返回一个字符?

标签 vim

在 vim 中,我一直很好奇为什么按 esc 键会使光标后退一个字符(通常)。例如,如果我输入:

This is |

当光标位于 | 时,在我完成输入并按 esc 后,该行将如下所示:

This is|

要从我离开的地方重新输入,我必须按 a 而不是 i。为什么这是 vim 中的预期行为?

最佳答案

我在 this question 中找到了以下答案在 Unix & Linux Stack Exchange 上:

In insert mode, the cursor is between characters, or before the first or after the last character. In normal mode, the cursor is over a character (newlines are not characters for this purpose). This is somewhat unusual: most editors always put the cursor between characters, and have most commands act on the character after (not, strictly speaking, under) the cursor. This is perhaps partly due to the fact that before GUIs, text terminals always showed the cursor on a character (underline or block, perhaps blinking). This abstraction fails in insert mode because that requires one more position (posts vs fences).

Switching between modes has to move the cursor by a half-character, so to speak. The i command moves left, to put the cursor before the character it was over. The a command moves right. Going out of insert mode (by pressing Esc) moves the cursor left if possible (if it's at the beginning of the line, it's moved right instead).

I suppose the Esc behavior sort of makes sense. Often, you're typing at the end of the line, and there Esc can only go left. So the general behavior is the most common behavior.

Think of the character under the cursor as the last interesting character, and of the insert command as a. You can repeat a Esc without moving the cursor, except that you'll be bumped one position right if you start at the beginning of a non-empty line.

感谢原作者。

如果您想编辑此行为,您可以遵循@ib 的建议。在 this answer :

Although I would not recommend changing the default cursor mechanics, one way of achieving the behavior in question is to use the following Insert-mode mapping.

:inoremap `^

Here the Esc key is overloaded in Insert mode to additionally run the `^ command which moves the cursor to the position where it had been the last time Insert mode was left. Since in this mapping it is executed immediately after leaving Insert mode with Esc, the cursor is left one character to the right as compared to its position with default behavior.

Unlike some other workarounds, this one does not require Vim to be compiled with the +ex_extra feature.

关于vim - 为什么按 escape 返回一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61243822/

相关文章:

bash - 如何在 :! bash 后重新进入 Vim?

vim - 如何在 Vim CtrlP 中自定义线条高亮颜色?

Vim 不断变化 expandtab

vim - 如何在 vim 编辑器中使用 sed 来替换模式?

vim - 让 vim 在 ~/.vim/bundles 中使用新插件的最快方法

Linux 中通过 Emacs/Vim 进行 Haskell 编码 : organising files into projects

vim - 如何阻止Vim创建/打开新文件?

VIM 总是使用标签页

search - vim、ed 或 sed 对给定文件进行多次搜索和替换

windows - 我需要在我的电脑上手动设置 $VIMRUNTIME