3

I'd like to experiment one of the tip by Drew Neil in his Vimcast, that is to "escape" regularly, so that it gives more granularity to the "undo" command.

For example, I write one short sentence. Then I press Escape. Then I press a to continue my editting and write another sentence. So when I press my final escape, I can press u several times and only one sentence at a time will disappear, not the whole text.

I was wondering if I could remap this simple sequence, (for example with kj):

inoremap kj <esc>a

The key remapping works (it feels like I didn't exit insert mode), but the problem is, when I use kj to mark a pause after a sentence, then if I write another sentence and press escape, then pressing u will erease my two sentences at once, and therefore I don't benefit the granularity I was looking for in the first place.

Why is it pressing kj doesn't have the same effect on the undo command compared to pressing manually escape + a ?

And, is there a way to do the remaping with the desired effect?

2
  • 1
    does ctrl-g u in insert mode already do what you want?
    – Mass
    Commented Jan 25, 2019 at 16:58
  • @Mass Yes it's exactly the desired effect. Remapping inoremap kj <c-g>u is working with the undo command too, that's great. Thank you so much! I can accept this answer if you may add it. I have no idea, what ctrl-g means in insert mode, it seems ctrl-g + k and ctrl-g + j go up and down one line while breaking the undo tree.
    – Yoric
    Commented Jan 25, 2019 at 17:30

1 Answer 1

1

How about inoremap jk <c-O><esc>? <c-O> in insert mode is executing one command and return to insert mode. So it executes <esc> and return to insert mode.

Though it is unable for me to explain precisely because it is depended on Vim's specification, I guess that it is because when you map <esc> to some keys, it will be executed as one general command, not as <esc>, and Vim records one set of strokes for insert mode from enter to it to press <esc>. The command we define seems not to be regarded as <esc> even if it includes <esc> inside.

See this link and :help i_CTRL-G_u

1
  • Thanks @Szkieletor for your awsome answer. The <c-O><esc> sequence is perfect for this purpose in insert mode. The link and the help reference you provided are much appreciated (I couldn't find it)
    – Yoric
    Commented Jan 31, 2019 at 12:43

Your Answer

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

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