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.
I am responding to this question four years later because the answer provided is a partial solution that does not completely address the original question.
I have the exact same desire as olibre, and wanted to come up with a complete solution. I come at it from a different perspective -- I'm used to Emacs line editing in bash -- but the issue is the same. I want to carry that over into Vim insert mode (only).
The following addresses all of the keybinding requests in the original question posed by olibre, plus few extras:
" Mimic Emacs Line Editing in Insert Mode Only
inoremap <C-A> <Home>
inoremap <C-B> <Left>
inoremap <C-E> <End>
inoremap <C-F> <Right>
" â is <Alt-B>
inoremap â <C-Left>
" æ is <Alt-F>
inoremap æ <C-Right>
inoremap <C-K> <Esc>lDa
inoremap <C-U> <Esc>d0xi
inoremap <C-Y> <Esc>Pa
inoremap <C-X><C-S> <Esc>:w<CR>a
The only bindings I cannot seem to get to work at this point are <Alt-B>
and <Alt-F>
, which would skip entire words. Whenever I bind <Esc>
, <Alt>
, or <Meta>
combinations, gVim (v7.4 on Windows) outputs accented characters. Please feel free to edit this answer if you have a solution to this issue. I have gotten Alt combinations to work based on another post's suggestion to just use the special characters. I have inserted them above.
Edit
-- (edit is not by original answer poster) --
I used the following under Neovim and Windows 10. You might need to change the "A" into "M" on other OSs as "alt" might be "meta" elsewhere. I haven't played with it enough.
inoremap <A-x> <Esc>:
inoremap <A-f> <Esc>lwi
inoremap <A-b> <Esc>bi
inoremap <A-S-f> <Esc>lWi
inoremap <A-S-b> <Esc>Bi
Best Answer
Sometimes the
vi
command is an alias forvim
and when called asvi
enables its vi-mode.Even in traditional mode backspace is deleting the character, but does not display it as deleted immediately. (After pressing ESC the characters are gone.)
Guess you have to choose between using
vi
which comes with the described behavior or usingvim
which is able to do it the way you expect it.