In insert mode <c-e> and <c-y> copy a character at a time from the above line.

I generally use these when working with aligned bits of text inside a program and want to copy to the end of the line more often than anything else.

Is there a way to do this?

So far I've been using:

inoremap <c-e> <esc>lj"py$hk"pp`[
inoremap <c-y> <esc>lk"py$hj"pp`[

and a slightly better version to that doesn’t stop executing at the h when running at the 0th column.

inoremap <c-e> <c-o>:stopinsert<cr>j"py$kv"pp`[
inoremap <c-y> <c-o>:stopinsert<cr>k"py$jv"pp`[

But I'm not thrilled with either of those bindings since it clobbers a named register, seems dependent on set virtualedit.

Is there a simpler/more elegant way to copy the trailing portion of the above line?

  • 1
    What's the "strange behavior"? And if you don't want to use a named register why not something like inoremap <c-e> <c-o>:norm! jly$kp[<CR>` – B Layer Dec 15 '18 at 2:17
  • Strange behavior is the command stops as soon as h fails. Ah, I also don’t want to clobber the default register either. I was hoping that this configuration desideratum would be common enough to have a setting or plugin or something similar. – Gregory Nisbet Dec 15 '18 at 2:30
  • 1
    Is there a reason you can't make use of Vim's full functionality (e.g. unnamed register, virtual edit)? Seems kind of...well...masochistic. ;) – B Layer Dec 15 '18 at 3:05
  • 1
    yyp and edit, or <C-x><C-l> if the lines are exactly the same – D. Ben Knoble Dec 15 '18 at 3:42
  • 1
    If you want to avoid registers perhaps some vimscript...e.g. for <c-e> : let b:col = getcurpos()[2] | call setline(".", strcharpart(getline("."), 0, b:col) . strcharpart(getline(getcurpos()[1] + 1), b:col)) . for <c-y> change "+ 1" to "- 1". (I'm ignoring any exit/entry from/to Insert mode for simplicity's sake.) – B Layer Dec 15 '18 at 4:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.