8

From help :help i_ctrl-a:

CTRL-A      Insert previously inserted text.  {not in Vi}

Let's say I have an empty buffer and I'm in normal mode.
I type i to go in insert mode.
I type (), then move the cursor one character to the left by hitting <C-h> (which I mapped to <left> in insert mode), type foo, and go back in normal mode by hitting escape.

The buffer contains: (foo)
I thought, wrongly, that the previously inserted text would be (foo) but it's not.
The previously inserted text is only foo which can be checked by typing :registers . or hitting <C-a> in insert mode again.

Why does hitting an arrow key reset the previously inserted text ?
Is there a way to make the dot register store all the text that was inserted the last time I went in insert mode even though I hit an arrow key ?

1 Answer 1

7

In insert mode, the left arrow key actually exits insert mode, moves the cursor, then re-enters insert mode. That breaks your change into two changes: inserting () and inserting foo. This behavior is discussed under

:help ins-special-special

The solution is to remap the left arrow key to include CTRL-G U. The example in the Vim Reference Manual is hard to find because it's not directly tagged. First go to

:help i_CTRL-G_U

then scroll down 42 lines to "An example for using CTRL-G U:" For the left arrow key, use this mapping:

inoremap <Left> <C-G>U<Left>

It seems to work, but the display of the . register by the :registers command looks weird after executing your (foo) example. On my Windows machine, it looks like this:

".  ()^GU<80>klfoo
3
  • 1
    Thank you very much ! That was bothering me since a long time, and never found the solution. I modified my vimrc with inoremap <C-h> <C-g>U<Left> and inoremap <C-l> <C-g>U<Right> and your solution works perfectly ! By the way, on my linux machine I've got the same weird output as you for the :registers command, so it's not coming from windows.
    – saginaw
    Dec 22, 2015 at 1:28
  • 2
    <c-g>U came with a recent patch. That may explain why you missed it so far. Dec 22, 2015 at 8:03
  • Do you think there's a way to make it work with Ctrl+Right/Left as well? I tried analogous mappings but they don't seem to work. Feb 6, 2018 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.