VI Editor: Move to EOL instead of last character
Asked Answered
S

2

15

I'm most often finding myself having to work with plain old vi on minimalistic terminals that tend to act differently than the vim on big distros, and so the behavior trips me up.

What I want to know is not how to move to the last character in the line, but one character past that. Because typing $ does NOT move the insertion cursor to the last character in the line, and this is easily proven. I'm using vi on MSYS right now.

If I type in the line

This is a test

and hit esc, $, i, and <enter>, I will get the following:

This is a tes
t

This shows that the insertion cursor was not put at the end of the line, but one to the left of the last character in the line (Imagine the box being a line like in Notepad++. The box is over the top of the letter, signifying that the character will be inserted in the area that makes up the box's left side), and I know this because if it were really at the end ("end" meaning to the RIGHT of the last character) the last character would not be moved to its own line such as what you see above.

I don't want this behavior. It's too much a pain for me to type one extra character every time I press enter just to ensure I don't have to retype my words. This kills my productivity.

How do I fix this? Is it a setting I can tweak in .exrc?

Skald answered 25/4, 2014 at 14:26 Comment(3)
This is a comment not an answer because I don't know how much of this is applicable to original vi (I use vim), but when you use i it inserts before the character you are on. Use a to insert after (append), and A to append at the end of the current line ("after" $).Frazzled
I don't know for sure about plain old vi but can't you type A in command mode? That should put you after the last character in insertion mode. If that doesn't work type <Esc>$a which should do the same thing.Binghi
From my knowledge both i and a put you in insertion mode, the difference is that i starts you before the current character and a starts you after.Binghi
U
38

The normal mode command for entering insert mode at the end of the line is A.

More generally, i enters insert mode before the current character so what you get is perfectly in line with what you do: $ puts the cursor on the last character and i enters insert mode before the last character. That's what you ask Vim to do and that's what it does.

If you want to enter insert mode after the current character, the right command is a so what you should have done instead of $i (which can't do what you want, whether you are in vi or vim) is $a for which there is a cool shortcut: A.

Unbar answered 25/4, 2014 at 14:46 Comment(4)
I'm less confused as to why this isn't the default behavior of vi and more confused as to why the hell I didn't learn this sooner after using vi for like two years.Skald
@DylanLaCoursiere, iaIA<Esc>:w:q are the first commands one is supposed to learn.Unbar
@romainl, how did you learn vim? Do you have a good resource for beginners?Untouchable
@jchook, 1. If you didn't already, do $ vimtutor as many times as needed to get the basics right. 2. As instructed at the end of vimtutor, level up to the user manual: :help user-manual. It will guide you progressively through every feature, from basic to advanced. This is not a novel, though: go at your own pace, skip chapters, come back to them later, and, most importantly, experiment along the way. 3. Keep an eye on anti-patterns and inefficient actions, find improvements, practice. Rince. Repeat.Unbar
J
11

I am guessing you want to use o to "open" a new line. However I have provided some ways to insert text via vi

Insert commands

It should be noted that vi/vim's cursor sets on top of a character not between character like most editors.

  • i insert text before the cursor
  • a append text after the cursor
  • I insert text before the first non-blank in the line
  • A append text to the end of a line
  • o begin a new line below the current one and start insert mode
  • O begin a new line above the current one and start insert mode
  • S/cc delete the current line and start insert.
  • c{motion} delete {motion} and start insert mode. Read as change
  • C Delete from current position to end of line and start insert mode
  • s remove character under cursor and start insert mode
  • r replace one character
  • R start replace mode. Think of it as overwrite

For help on any of these command type :h {command} so help on A would be :h A

Jevons answered 25/4, 2014 at 15:11 Comment(1)
I think OP wants A but +1 anyway for the comprehensive list.Frazzled

© 2022 - 2024 — McMap. All rights reserved.