Cursor positioning when entering insert mode
Asked Answered
F

7

54

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.

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)?

Foreclose answered 9/9, 2010 at 12:12 Comment(0)
A
84

it is a little more clear if you use gvim, where the cursor changes.

insert mode in gvim has the cursor as an I-beam, since the next letter you type will be inserted after the |. 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). 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)

Let's say you have t[h]is text





if you pressed i at this point, the cursor would look like this (in gvim) (| being the insert mode cursor)

Let's say you have t|his text





if you pressed a instead of i, it would look like this

Let's say you have th|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.

Anniceannie answered 9/9, 2010 at 12:24 Comment(4)
Key press 'a' is what I have been longing for for too long. Thank you Matt.Invasive
Thank you very much for pointing out 'a'. I'll remember this day.Burnout
How can I start inserting one character behind the one currently highlighted by the cursor?Geiger
this is like most important thing and not one tutorial mentioned it! Super important for pythonNatiha
D
28

I'm not quite sure of the reasoning behind it, but you can work around it by pressing:

Shift + a
Divider answered 9/9, 2010 at 12:15 Comment(1)
This works, and the reason is A. You can read more therePlebiscite
U
11

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.

There's actually no need to move the cursor in command mode behind the last letter, if you want to e.g. append you can press a which puts the cursor behind the letter in insertion mode.

Unintentional answered 9/9, 2010 at 12:23 Comment(0)
M
11

You might be interested in the option virtualedit and the following value:

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.

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 ;-)

Muckworm answered 12/9, 2010 at 19:16 Comment(0)
F
5

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). You can press a to enter insert mode and start editing after the current character in this situation (rather than i).

Florettaflorette answered 9/9, 2010 at 12:23 Comment(0)
C
4

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.

So, use A to quickly start typing at the end of the line.

Chile answered 3/2, 2021 at 19:22 Comment(0)
S
1

I suggest

:set virtualedit=onemore
:inoremap <Esc> <Esc>`^
Siple answered 16/6, 2014 at 22:18 Comment(2)
Why do you suggest change the i command (to enter the edit mode)?Diocletian
To prevent the cursor from moving one character left.Siple

© 2022 - 2024 — McMap. All rights reserved.