Why does pressing escape go back one character? - vim

In vim, I've always been curious why pressing the esc key causes the cursor to go back by one character (usually). For example if I've typed:
This is |
With the cursor being the |, after I have finished typing and press esc, the line will look like:
This is|
And to re-type from where I left off I would have to press a instead of i. Why is this the expected behavior in vim?

I found the following answer in this question on the Unix & Linux Stack Exchange:
In insert mode, the cursor is between characters, or before the first
or after the last character. In normal mode, the cursor is over a
character (newlines are not characters for this purpose). This is
somewhat unusual: most editors always put the cursor between
characters, and have most commands act on the character after (not,
strictly speaking, under) the cursor. This is perhaps partly due to
the fact that before GUIs, text terminals always showed the cursor
on a character (underline or block, perhaps blinking). This
abstraction fails in insert mode because that requires one more
position (posts vs fences).
Switching between modes has to move the cursor by a half-character, so
to speak. The i command moves left, to put the cursor before the
character it was over. The a command moves right. Going out of
insert mode (by pressing Esc) moves the cursor left if
possible (if it's at the beginning of the line, it's moved right
instead).
I suppose the Esc behavior sort of makes sense. Often,
you're typing at the end of the line, and there Esc can
only go left. So the general behavior is the most common behavior.
Think of the character under the cursor as the last interesting
character, and of the insert command as a. You can repeat
a Esc without moving the cursor, except that
you'll be bumped one position right if you start at the beginning of a
non-empty line.
Credits to the original author.
If you would like to edit this behavior, you could follow the advice from #ib. in this answer:
Although I would not recommend changing the default cursor mechanics,
one way of achieving the behavior in question is to use the following
Insert-mode mapping.
:inoremap `^
Here the Esc key is overloaded in Insert mode to
additionally run the `^ command which moves the cursor to the
position where it had been the last time Insert mode was left. Since
in this mapping it is executed immediately after leaving Insert mode
with Esc, the cursor is left one character to the right as
compared to its position with default behavior.
Unlike some other workarounds, this one does not require Vim to be
compiled with the +ex_extra feature.

Related

How to get delete-button behavior in vim?

When using vim in insert mode (or not using vim at all), pressing the delete-button deletes a character TO THE RIGHT of the cursor. How can I get this behavior in vim when in normal mode?
Pressing x is not the right answer as it deletes the selected character, not the character to the right.
In the Insert mode it's just for you the cursor looks "kinda between characters". But in fact, your computer sees it being right on the character you are deleting.
In the Normal mode you normally have the bar-cursor, so it looks the way it works.
Now, of course, you can remap your "x" to delete the char to the right, but it will only add a confusion. So, please, don't. Train your mind to see the things in the proper light instead.

How to perform Ctrl+Del in Vim while in insert mode?

Does anyone know how to delete the word in front of the cursor while in insert mode in Vim? Most systems use Ctrl+Del to do this.
I know to delete the word behind the cursor in Vim is Ctrl+w while in insert mode, but I don't know how to delete forward.
Add this to your ~/.vimrc (for VIM or GVIM):
imap <C-D> X<Esc>lbce
; 1 2 3 4 5 ; just a comment for the further description
(In GVIM, <C-Del> also works.)
Description
Set the following binding on Ctrl+D in the insert mode.
Add a dummy letter to deal with words that initially contain only one character.
Leave the insert mode. (The cursor is on the added letter now.)
Move to the beginning of the current word.
Remove characters from the current position to the next end-of-word (i.e., from the first character of the current word to its last character). Then enter the insert mode again.
Behavior
If the cursor was on a character of a word or immediately after the last character of a word (but not at the end of the current line), then this word will be removed.
Otherwise, if there's a word to the left (on the same line), then it will be removed (without any blank characters).
Otherwise (when there's no next word) the behavior is undefined. (Feel free to update the answer to cover these cases too.)
If a word is removed, then the cursor will be at the same position as the first character of the word was.
Use a single normal mode command, whilst in insert mode.
To do this, whilst in insert mode, type
Ctrl+o
This tells vim to accept exactly 1 command as if it were normal mode. Then press
dw
to delete a word.
The accepted solution is not exactly correct, right? It does delete backwards but not forward
In nvim this works:
imap <C-Del> X<Esc>ce
You could also add below to your .vimrc
inoremap <C-Del> <C-o>dw

Why can't I get `< and `> to work in MacVim?

I have this line in a MacVim buffer:
abcdefghijklm
I then type fev3l<esc>, to move the cursor to e, visually select the text efgh, and go back to normal mode.
I then press `< , and expect the cursor to move to e. Instead, it moves to a. If I then press `>, the cursor moves to h. Where the cursor actually moves seems a bit arbitrary, but I haven't figured out if there is a consistent pattern.
When doing this in /usr/bin/vim on the command line, I get the expected behaviour.
Looking at :nmap, I do not have ` mapped.
What could I be doing wrong?
You should check your keyboard layout.
On a non-US layout that contains diacritics, keys that correspond to diacritic marks will behave differently. Instead of being produced right away, such keys now are meant to be used in combination with another one to produce a letter with a diacritic, which may alter their expected behavior with programs.

Opposite of newline in vim

In vim, is there a command to delete the newline, and all empty space behind the cursor?
Say I stand in the middle of a text in insert mode and press Enter, what command would the reverse what I just did?
A) An example:
"some code{ in here }"
B) After pressing Enter:
"some code{
in here }"
Now pressing backspace will delete one space of the indentation. I would rather have it delete all indentation, and jump back to A.
Can this be done in a command or by doing some remapping to the backspace key?
It's tragic how unknown the J command is. It joins lines in normal mode.
In insert mode, you can press <C-U> twice; first, it'll delete the indent before the cursor, then it'll join with the previous line. Note that this requires
:set backspace=indent,eol,start
did you try J (uppercase) ? it will give exactly what you want.
"some code{ cursor on this line, pressJ
in here }"
You can do ᴇꜱᴄ, K, Shift+J.
K jumps up to the previous line and Shift+J joins the two lines.
However, with properly configured indentation and syntax, a backspace doesn’t just delete a space, it deletes the full previous indentation block.
One easy way is up one line, to end of that line and just delete. As long as you still are in insert mode it will do the same thing as J when deleting at the last position - like most other editors. For me that is the quickest alternative because I'm used to it from other editors.
That is: ↑, End, Delete (when still in insert mode)
One quick alternative (the VIM-way) is (when still in insert mode):
↑, Ctrl+o, J (when still in insert mode)
(Ctrl+o is used in insert mode to enter one normal mode command.)
It's also possible to use a remapping of the backspace key:
inoremap <expr> <bs> getline('.')[:col('.')-2]=~'^\s\+$' ? "<c-u><c-u>" : "<bs>"
Note that this mapping completely overrides the normal behavior the backspace key. This will only be useful when you don't intend to use its normal behavior. This is not recommended if you can easily access the other options (c-u or J)
However, (as far as I know) there's no way to distinguish between manually added leading white spaces and auto indent. If you use noexpandtab, you can edit the regex to only match tabs.
This also does not work in some modes of auto-indent (for example, in block comment in C, vim automatically start a new line starts with *)

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