I have created the following key-binding for copying all text in the buffer to the system clipboard:

nnoremap <Leader>bA gg"*yG

However, it moves the cursor, so I need to append <C-o><C-o> in order to jump back to the previous location.

How can I move back to the previous location without appending multiple <C-o> for a key-binding?

  • 3
    Instead of copy and pasting by moving, use the following ex command :%y+ – Christian Brabandt Oct 15 '20 at 10:46
  • Thank you. I only used the other approach, since I wasn't aware where to place the register +. I initially tried :%y"+ etc. I created the mapping noremap <Leader>by :<c-u>%y+<cr>, do you like it, or could I improve it? – Shuzheng Oct 15 '20 at 10:58
  • 1
    do not use the " on the ex command line, since it is being considered the comment char. Please also have a look at the help: :h :y. It doesn't mention to use the " in the ex commandline. – Christian Brabandt Oct 15 '20 at 11:53

As pointed out in a comment, perhaps the best way is to not move the cursor:

:%yank *

(or +, if you prefer that register). Can be shortened to :%y* if you prefer.

See :help :yank for details.

  • 1
    Thanks, you need to prefix your command by : – Shuzheng Nov 2 '20 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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