2

Once I've asked a general question about special characters, but I've not really pulled anything out of it.

So now I'm asking a very specific question.

I want to have Ctrl+e and Ctrl+y behave in insert mode as they do in normal mode.

(Yes, I have looked at :help i_CTRL-E and :help i_CTRL-Y, but I have no idea when such a functionality will ever be useful to me, so I'd happily drop it in favour of normal mode CTRL-E and CTRL-Y.)

Here's my main failed attempt:

inoremap <c-e> <esc>:<c-u>exe "normal \<c-e>"<cr>a

but clearly there are a few variations of it where I've tried to more \, or to insert Ctrl+e by hitting it after Ctrl+v.

And even if the above command worked, I think it would have the problem of screwing up the undo sequence because it exits insert mode and then re-enters it.

1 Answer 1

2

EDIT

As @D.BenKnoble pointed out this is the simplest way:

inoremap <C-E> <C-O><C-E>

Original answer

Try using <cmd>...<cr> instead:

inoremap <c-e> <cmd>exe "normal! \<c-e>"<cr>

:h <cmd> excerpt:

*<Cmd>* *:map-cmd*

The special text <Cmd> begins a "command mapping", it executes the command
directly without changing modes.  Where you might use ":...<CR>" in the
{rhs} of a mapping, you can instead use "<Cmd>...<CR>".
Example: 
    noremap x <Cmd>echo mode(1)<CR>

This is more flexible than `:<C-U>` in Visual and Operator-pending mode, or
`<C-O>:` in Insert mode, because the commands are executed directly in the
current mode, instead of always going to Normal mode.  Visual mode is
preserved, so tricks with |gv| are not needed.

PS,

Your mapping could be also written as

inoremap <c-e> <c-o>:exe "normal \<lt>c-e>"<cr>

Where you change < to a <lt> as otherwise \<c-e> is literally interpreted by a command line your are in when mapping is in effect. (and <C-e> is goto end of cmdline)

11
  • Would you believe that I knew about this and probably even used it once or twice? Ufff, things just slip out of my mind so easily! Thanks for the perfect solution! :D
    – Enlico
    Sep 19, 2022 at 11:06
  • But out of curiosity, can my original attempt be fixed by writing appropriately the stuff inside "..."? Or is it just not possible to do it that way?
    – Enlico
    Sep 19, 2022 at 11:15
  • to be fair, it looks like a vim bug to me, @Enlico
    – Maxim Kim
    Sep 19, 2022 at 11:26
  • What bug? I mean, do you refer to a software bug, or to a design bug (as in, Vim is behaving as it was designed to, even if nobody likes this behavior)? I'm asking because I would report it... I mean, for what is worth, considering your proposed solution is better anyway.
    – Enlico
    Sep 19, 2022 at 11:30
  • 1
    Er, isn’t this just C-o C-e? And you may want to include an expr-map in case the popup menu is showing.
    – D. Ben Knoble
    Sep 19, 2022 at 12:19

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.