0

So I know about Copy all the lines to clipboard, but this only applies for stuff in the editor.

What about copying stuff from the vim command line?

Community
  • 1
  • 1
InquilineKea
  • 853
  • 4
  • 20
  • 36

2 Answers2

2

Pressing q: in normal mode or <c-f> while entering a command brings up the command-line window, with is filled with your command history. This is just another window, so from there you can use the "*y operator as described in the linked question.

Ismail Badawi
  • 31,611
  • 6
  • 76
  • 92
1

I have this line in the "Sharing is caring" section of my vimrc:

command! CMD let @+ = ':' . @:

It simply puts the last command executed in the command-line into the clipboard register, ready for pasting elsewhere.

Explanation:

  • The last command is always saved in the ": register, it is one the few read-only registers.

  • The "+ register represents your system's clipboard, it is possible to read from and write to it.

  • The syntax for working with the content of a register is:

    :let my_variable = @a    " put the content of register "a in my_variable
    :let @a = my_variable    " put the content of my_variable in register "a
    
  • So we can put the content of one register into another (not read-only) one:

    :let @a = @b
    
  • In VimScript, string concatenation is done with a .:

    :let foo = "foo"
    :let bar = "bar"
    :echo foo . bar
    foobar
    
  • Because registers are strings we can do all kinds of operations on them before putting their value in some variable or register:

    :let @a = "foo"
    :let @b = @a . "bar"
    :echo @b
    foobar
    
  • In my command, I prepend the ": register with a colon for clarity:

    :let @+ = ':' . @:
    
romainl
  • 158,436
  • 18
  • 236
  • 264