简体   繁体   English

进入插入模式时的光标定位

[英]Cursor positioning when entering insert mode

When I switch to command mode in Vim, the cursor seems to move one character back when it's at the end of or on a word, and when I go to the end-of-line with $ , it does not go to the actual end of line but one character before the end of the last word, and l ("el") does not move it forward and I have to use the arrow key to get there.当我在 Vim 中切换到命令模式时,当光标位于单词末尾或单词上时,它似乎向后移动一个字符,而当我使用$到行尾时,它不会移动到实际末尾行,但在最后一个单词的末尾前一个字符,并且l ("el") 不会向前移动它,我必须使用箭头键才能到达那里。

I haven't been able to find documentation of this behavior, but this seems strange to me.我一直无法找到这种行为的文档,但这对我来说似乎很奇怪。 What's the reasoning behind this (for my own curiosity), and how can I get around it (or deal with it)?这背后的原因是什么(出于我自己的好奇心),我该如何解决(或处理它)?

it is a little more clear if you use gvim, where the cursor changes.如果您使用 gvim,光标会发生变化,则会更清楚一些。

insert mode in gvim has the cursor as an I-beam, since the next letter you type will be inserted after the |. gvim 中的插入模式将光标作为 I 型梁,因为您键入的下一个字母将插入在 | 之后。 normal mode has the block cursor, because the next thing you type may just effect the letter that is currently highlighted (like if you use x, s, etc).正常模式具有块光标,因为您接下来键入的内容可能只会影响当前突出显示的字母(例如,如果您使用 x、s 等)。 So insert mode is actually adding text, but normal mode is modifying text in some way.所以插入模式实际上是添加文本,而普通模式是通过某种方式修改文本。

So in normal mode, jumping to the end of the line really just means the last character, since that is the last thing that is possible to be modified.所以在普通模式下,跳到行尾实际上只意味着最后一个字符,因为这是最后一个可以修改的东西。 in insert mode, the cursor goes passed the last character, since it is possible to add things afterwards.在插入模式下,光标经过最后一个字符,因为之后可以添加内容。

One thing to keep in mind is that you can control which side of the block you end up on going from normal mode to insert mode要记住的一件事是,您可以控制最终从正常模式转到插入模式的块的哪一侧



([] means that the block cursor is over that h) ([] 表示块光标在那个 h 上)

Let's say you have t[h]is text假设您有 t[h] 是文本





if you pressed i at this point, the cursor would look like this (in gvim) (| being the insert mode cursor)如果此时按下 i,光标将如下所示(在 gvim 中)(| 是插入模式光标)

Let's say you have t|his text假设你有 t|his 文本





if you pressed a instead of i, it would look like this如果你按下 a 而不是 i,它看起来像这样

Let's say you have th|is text假设你有这个|is text





Another thing to keep in mind (as pavanlimo mentioned), from normal mode you can go to insert mode with your cursor just before the first character of the line, or just after the last character, with shift-I or shift-A.另一件要记住的事情(如 pavanlimo 提到的),从正常模式中,您可以使用光标在行的第一个字符之前或最后一个字符之后使用 shift-I 或 shift-A 进入插入模式。

我不太确定它背后的原因,但你可以通过按下来解决它:

Shift + a

You might be interested in the option virtualedit and the following value:您可能对选项virtualedit和以下值感兴趣:

set virtualedit=onemore

With this option you can move the cursor one character over the end of the line and then press i to insert mode after the last character.使用此选项,您可以将光标移动到行尾一个字符,然后按i在最后一个字符后插入模式。

This solves the issue in a way but personally I find this behavior a bit odd.这在某种程度上解决了这个问题,但我个人觉得这种行为有点奇怪。 Only in a few cases you encounter the problem so it might be worth ignoring it ;-)只有在少数情况下您会遇到问题,因此可能值得忽略它;-)

That's because all commands you use affect the letter the cursor is over.那是因为您使用的所有命令都会影响光标所在的字母。 If wouldn't make sense to press x (delete 1 letter) behind the actual letter.如果在实际字母后面按x (删除 1 个字母)没有意义。

There's actually no need to move the cursor in command mode behind the last letter, if you want to eg append you can press a which puts the cursor behind the letter in insertion mode.实际上没有必要在命令模式下将光标移动到最后一个字母后面,如果你想例如追加,你可以按a在插入模式下将光标放在字母后面

It is implementation-dependent on whether the cursor can move past the end of the line (at least it is an option in my editor's VIM emulation mode).它取决于光标是否可以移动到行尾(至少它是我编辑器的 VIM 仿真模式中的一个选项)取决于实现。 You can press a to enter insert mode and start editing after the current character in this situation (rather than i ).在这种情况下,您可以按a进入插入模式并在当前字符之后开始编辑(而不是i )。

I suggest我建议

:set virtualedit=onemore
:inoremap <Esc> <Esc>`^

pressing i will enter the insert mode before the cursor a after the cursor I before the first non empty character in the line A at the end of the line.i将进入插入模式,在光标a之前,在光标I之后,在行尾A行中的第一个非空字符之前。

So, use A to quickly start typing at the end of the line.因此,使用A在行尾快速开始输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM