5

How to copy command string in the ex mode into clipboard?

enter image description here

showkey
  • 482
  • 42
  • 140
  • 295

2 Answers2

6

You can turn the command-line mode into a full Vim buffer named the command-line window by pressing <C-f> after entering in command-line mode with :, or enter into it directly via q:. There, you can yank as usual; e.g. via "+yy or :yank +<CR>. (This is also great for other complex edits!)

The last command-line is in register :; :let @+ = @:<CR> will copy that to the clipboard.

The least Vim-like approach would be selecting with the mouse; on Linux, the text is then in the X primary selection; on Windows, you can then use Edit > Copy to put it in the clipboard.

aturegano
  • 936
  • 15
  • 30
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Are you sure that `` does the work you mentioned? If I recall properly, this is for next page. – aturegano Feb 23 '23 at 21:08
  • 1
    @aturegano In Vim, everything depends on the active mode. `` is _next page_ in normal mode, but in command-line mode opens the command-line window. – Ingo Karkat Feb 24 '23 at 10:22
0

Go to the line to be copied; be in Command mode, not Input mode.

Create a "macro" by copying the line into a named buffer. I like to use m for macro:

"m

Yank the line into the buffer you just named:

yy

Now (still in Command mode) you can "run" the buffer as a "macro" using the standard @ to address the named buffer:

@m

If you run it and there is a mistake, simply press u to "undo" and correct the line

"myy can become muscle-memory after a short time.

~ A more detailed example ~

Make some data just by copying and pasting these commands.

No need to type them:

$ printf "
TRUE(1)                           TRUE(1)

N\bNA\bAM\bME\bE
  t\btr\bru\bue\be - return true value

S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
  t\btr\bru\bue\be
" > zap



$ cat <<'EOF' >> zap
This part could just all be typed manually
otherwise append the " before copying into buffer
:%!sed "s/.$(printf '\b')//g
EOF

Open the file. It will likely look pretty funky:

$ vi zap

Go to the end of the buffer G

Next, go into Input Mode, o and enter your "macro" on an empty line, exactly as if you were typing it in Command Mode, including the leading colon to make it easy to spot.

:%!sed "s/.$(printf '\b')//g"

We already added most of the line to save you the trouble. Add the closing double-quote at the end with <shift+a>

Always press <esc> to exit Input mode when you are done making edits.

Yank the whole line (with "Y" or "yy") into a named buffer. Again, we are going to simply use the name "m" for macro to make it easy to remember.

You can have multiple "macros" this way with different letter-names, but you need to remember them.

"myy

Once you are ready, use Command Mode to run the "macro", via @ and the buffer name.

@m

If done correctly, the text should instantly become "un-borked".

Obviously this is a short "macro", but it gives you the idea. The biggest advantage to doing it this way is that it is "proper" ex and vi usage as defined by POSIX so it should work without vim.

If you want to copy text into the OS-level clipboard, then just pipe it to xsel, xclip, pbcopy or the like in a similar manner.

Kajukenbo
  • 109
  • 1
  • 4