gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:31:12 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com