0

I have mapping for duplicating current line in insert mode:

inoremap <C-d> <Esc>`^mzYp`zi<Down>

I want to set up it in a way it does not change default " register by specifying some special register like a or 9:

inoremap <C-d> <Esc>`^mz"aY"ap`zi<Down>
inoremap <C-d> <Esc>`^mz"9Y"9p`zi<Down>

But yanking to this specific registers also affects default " register.

How to yank to some register without touching default register? Copying to what registers will not affect default register? Or how to make key mapping that will not affect default register?

1 Answer 1

1

This is much simpler and does not modify registers or bookmarks.

inoremap <c-d> <esc>:t.<cr>gi

Copying to what registers will not affect default register?

Default register is a pointer, not a real register. So no contents will get modified but the "pointer" will, as you're copying/deleting into any other register. So in mappings and scripts it's often better to use only commands and leave user's context intact.

5
  • This one is working the same as my inoremap <C-d> <Esc>^mzYpzi<Down>. It also saves cursor position. And even in border cases (start and end of line). Thanks! Commented Jun 16 at 15:43
  • Is it possible to configure it for normal mode also with preserving cursor position? nnoremap <silent> <c-d> :t.<cr> performs line duplication, but resets position to the start of line. Commented Jun 16 at 15:54
  • 1
    @AntonSamokat You can set a mark but, imo, it doesn't worth. Just get used to the fact the cursor in normal mode can jump to the start of line on numerous occasions.
    – Matt
    Commented Jun 16 at 16:26
  • Am I understand correctly that default register is a pointer to any of the last modified specific register? Commented Jun 16 at 16:59
  • 1
    @AntonSamokat Yes, you can browse through C source code to find exact conditions it gets changed.
    – Matt
    Commented Jun 16 at 17:06

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.