Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Vim position the caret one character off with $ (end of line)?

Tags:

vim

Observe a line in a Vim instance:

Now I hit $:

Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!

Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.

like image 419
Jonathan Dumaine Avatar asked Dec 31 '10 08:12

Jonathan Dumaine


People also ask

How do I insert a last line in vi?

Type A to add text to the end of a line. To see how this command works, position the cursor anywhere on a text line and type A . The cursor moves to the end of the line, where you can type your additions. Press Esc when you are finished.

How do I paste a line above and below the cursor line in Vim?

Pasting Copied Lines in VIMTo paste the lines below the cursor, simply enter p. To paste the lines just above the cursor, simply enter, P.


3 Answers

Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.

If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.

like image 175
Vortura Avatar answered Oct 19 '22 16:10

Vortura


Use a to append a character after the current.

Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.

Similarly shift-I to insert at the beginning of the line without first having to press ^.

like image 44
Alex Budovski Avatar answered Oct 19 '22 16:10

Alex Budovski


The cursor can't be between two characters, it is always on a character.

If you press $ then x, you will correctly delete the last printable character of the current line.

What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.

In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".

mnemonic for a : a for "append".

like image 4
Xavier T. Avatar answered Oct 19 '22 16:10

Xavier T.