Hi, sorry for this seemingly simple question - vim super-newbie here. 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)?
Thanks.
views:
98answers:
5I'm not quite sure of the reasoning behind it, but you can work around it by pressing:
Shift + 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). You can press a
to enter insert mode and start editing after the current character in this situation (rather than i
).
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.
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
Let's say you have t[h]is text
([] means that the block cursor is over that h)
Let's say you have t|his 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 th|is text
if you pressed a instead of i, it would look like this
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.
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 ;-)