Inserting at the end of a line in VIM - vim

So for a long time I've had a bad habit of going to the end of a line, hitting the i key and then hitting the right arrow to insert after that last character. However, this is extremely inefficient and impossible to do with hjkl.
What's a better way to insert at the VERY end of the line?

Use A command instead of i. The command appends text at the end of the line. See :help A.
In normal mode press A (uppercase). Vim will be switched to insert mode, and the cursor will be moved to the end of the current line ready for input.

The $ positions you at the end of line.
Then, to enter insert mode AFTER the end of line, press a.
So, simply typing $a while in command mode will do what you are asking.

Related

How can I move the cursor to the end of the line in insert mode?

Is there an existing command or a configuration for my .vimrc that will make CTRL+right arrow go to the end of the line in insert mode instead of going to the next line? Like in most common editors, that shortcut jumps word by word, but in vim it goes to the next line when the cursor is at the last word in the line.
If there is a different fast way to jump to the end of the line in insert mode, that would also be useful.
In general, the best way to navigate in vim is in normal mode, and not in insert mode. I suggest that instead of finding ways to navigate in insert mode, you use the power of normal mode as intended. There are many ways to do this, but here are a couple of suggestions:
Use CTRL+o while in insert mode to temporarily enter normal mode for the next command, and then $ to go to the end of the line (after which you will be returned to insert mode)
Use ESC to return to normal mode, and then A which both moves the cursor to the end of the line and enters insert mode
If you want to navigate word by word (in normal mode), you can use w to move to the next word, or e to move to the end of the next word.
The Home and End keys are usually interpreted by vim in insert-mode as beginning and end of line (like 0 and $), respectively.
This is documented in the ins-special-special topic, e.g.,
:h ins-special-special

How to run Vim shortcuts without exiting insert mode

For instance, I'd like to use $ to move to the end of the line, without going through Esc for it.
Just $, and not Esc then $.
Is there a way?
One solution could be: press another key(/combo), say, Ctrl+Shift, do what I want in "temporary normal mode", then when I release those keys I'm back in insert mode... if that's possible.
If you wanna perform one normal mode command in insert mode you can use Ctrl+O and then normal mode command. So Ctrl+O$ will move you to the end of line in insert mode (like End or Ctrl+OShift+A).
Is there any particular reason why you want to go to the end of the line? Vi is built to mainly be operated in normal mode, not insert mode (especially when it comes to movement).
For example, if you wanted to go to the end of the line and start adding text, you would type A (or ESCA if you were in insert mode at the time).
If you wanted to go to the end of the line so you could add a newline, you could type ESCo to start a new line and place you in insert mode.
If you gave us more context as to what you are ultimately trying to accomplish, we may be able to come up with a more efficient workflow.
inoremap $ <ESC>A
give this a try. It may do what you want, but make inserting $ harder.
Ctrl + Right
map! <C-Right> <Esc>A

moving text to next line in vi

There is a line in Shell script
f) SCREEN = TRUE
and I want to make it
f)
SCREEN = TRUE
I cannot do this very easily in vi. Obviously in a normal text editor enter command can shift the text after f) to the next line. What is the best way to do it in vi?
Press l to move the cursor right and j to move the cursor down to get the cursor to the S. Then press i to "insert" and hit enter to create a new line. Then hit esc to stop inserting and :wq to save and quit.
There are lots of basic vi command lists on the internet that might help.
I was trying to figure out if there was a way to do this in normal mode without pressing 3 buttons to insert, move the down line with enter and then escape to normal. I believe this is what OP may have been asking for. Not sure why there aren't any built in shortcuts for this.. Anyways I just ended up making a quick mapping in my .vimrc.
nmap <leader>j i<cr><esc>
With your cursor on the line, 0fSi<ENTER><ESC>
(0 Go to beginning of line, fS find 'S', i enter insert mode, <ENTER> insert some kind of newline, <ESC> exit insert mode)

vim: have Ctrl+right stop at EOL (just after last character of last word)

In windoze, when I use Ctrl+right, it moves word to word, and if I get to the end of the line, it stops on the last character of the last word + 1 (maybe that's the EOL character, not sure). In vim, when I reach the last word using Ctrl+right and hit it again, it skips the EOL and goes to the next word on the next line.
How would I get vim to follow the same method windows uses? Do I need to write a script for that?
Hit Escape, then Shift+A to append (at the EOL). If you fight vim and try to do all your movement in insert mode, vim will win.
I don't know how to change this, but did you know that, being in command mode, $ brings you to the EOL?
Yet another solution:
<C-o> $
while in insert mode. <C-o> allows you to make one normal-mode command and you stay in insert mode.

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.
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.
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.
I'm not quite sure of the reasoning behind it, but you can work around it by pressing:
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 ;-)
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 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).
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.
I suggest
:set virtualedit=onemore
:inoremap <Esc> <Esc>`^

Resources