23

I already know that you can copy something to the a register using: (plus a yank, delete, etc. command)

"a

I also understand that you can append to the a register using:

"A

In addition I understand how to copy something to the clipboard register using:

"+

What do I type to append to the clipboard register?

My Vim version:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan 2 2014 19:39:59) Huge version with GTK2 GUI.
0

4 Answers 4

13

There is no normal mode command for that but you can simply append to another register and :let the clipboard register to the content of that register:

"Ay
"Ay
"Ay
:let @+ = @A
6
  • 1
    Would I put that let in my .vimrc file? Commented Jul 4, 2015 at 21:24
  • 3
    Please note that let @+ = @A discards the type of register a. The correct way to copy registers is something like this: call setreg('+', getreg('a', 1, 1), getregtype('a')). Commented Jul 5, 2015 at 6:51
  • // , Does this actually append though? Commented Dec 29, 2015 at 18:19
  • // , Why do you list three "Ay commands? Commented Dec 29, 2015 at 18:20
  • It could have been four or twelve. Just an example.
    – romainl
    Commented Dec 29, 2015 at 19:35
9

You can call setreg('+', lines, 'a'), but as far as I can tell there is no way to do that with normal mode commands. You have to use one of the upper case letter registers for that.

2
  • Just using :let @+ .= 'XXX' seems to work? Commented Jul 4, 2015 at 6:56
  • 7
    @Carpetsmoker Yes and no. There has been a recent discussion about it on vim_dev and apparently you aren't supposed to do that, even though it may appear to work under some conditions. I presume it has to do with the register contents having a type (character / line / block), and string concatenation not doing the necessary bookkeeping. It doesn't make any difference anyway, it still isn't a normal mode command.
    – lcd047
    Commented Jul 4, 2015 at 7:02
0

Since clipboard is being used, it is obvious that X is being used. Issue of appending to clip board regularly occurs. If you are not using Linux, following is not for you. My solution consists of two additional programs, xclip and xbindkeys. Please see the following shell script which can be bound to a desired key combination in xbindkeys. Simply select the desired text and press the key combination to append the selection to the clipboard.

#!/bin/sh
APPND=1
if [ $# -gt 0 ] ; then
   if [ $1 -eq 1 ] ; then
      APPND=0
   else
      APPND=1
   fi
fi
if [ $APPND -eq 1 ] ; then
   echo "$(/usr/bin/xclip -o -selection clipboard) $(/usr/bin/xclip -o -selection primary)"|/usr/bin/xclip -i -selection clipboard
else
   echo "$(/usr/bin/xclip -o -selection primary) $(/usr/bin/xclip -o -selection clipboard)"|/usr/bin/xclip -i -selection clipboard
fi

Note: The above script is used to either insert in clipboard before of after its contents. the corresponding entries in my xbindkeys file is

"<complete path to the script>"
  Shift+Alt+c

"<complete path to the script> 1"
  Shift+Alt+d

So after selection, if we press Shift+Alt+c the selection is pasted at the end of the clipboard and if we press shift+Alt+d the selection is pasted before the current contents of the clipboard. Execute permissions should be given to the script. This is a universal solution for all x applications. I have verified it in gvim also.

1
0

I made a keymap which I use for appending. It yanks to the '0' register which is the default for yank and then appends it to your '+' register.

vnoremap <leader>y <Cmd>call AppendYank()<CR>

function! AppendYank()
    normal! "0y
    call setreg('+', getreg('+') . getreg('0'), getregtype('+'))
endfunction
2
  • 2
    The x register is not the default for yank. 0 is.
    – D. Ben Knoble
    Commented Feb 10, 2022 at 13:11
  • 1
    Thanks, I'll swap that. ':h yank' shows yank as '["x]y{motion}' which confused me.
    – declancm
    Commented Feb 10, 2022 at 21:02

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.