Why am I unable to copy to clipboard from vi editor?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
I use old servers so I've to use vi editor. Problem is that I need to copy logs frequently and it's totally painful to do so. I've to convert that to a file, do SCP then only be able to get the logs. But companies that I serve for are reluctant to provide their logs and there's a huge procedure where I've to write email, get approved and what not just to share that logs to developer.

https://superuser.com/questions/227385/how-do-i-select-all-text-in-vi-vim

I saw this post and I was so happy that I now just need to create a new file and copy all of that and paste to clipboard. (I work via remote desktop software like anydesk).

But none of the mentioned commands there worked for my case.

ggVG didn't work. The top answer too didn't work. When I typed ggVG, it indeed showed "Visual Editor" but it didn't copy the selected text at all.

Is it practically possible to do so in vi editor?

I'm on centos 7.
 


It’s actually really easy to do, once you know how. Vim uses registers for copy/paste operations. By default it uses its own set of internal registers. It doesn’t use the clipboard. But vim does have a special register for the system clipboard.

In order to copy to the system clipboard in vim, you need to copy to the + register.

So for example, if you want to copy an entire line of text to the system clipboard in vim:
  • Hit escape, to ensure you’re in command mode.
  • Use the motion keys hjkl to move to the line you want to copy, or use a motion command like {line number}G (where line number is the line you want to move to).

Once you’ve moved to the line you want to select:
- Use V to enter "visual line" selection mode. This will select everything on the current line.

- Type the command "+y - where " tells vim you want to use a register, + specifies the system clipboard register and y is the "yank" command/operation - which is the Vim equivalent to "copy". So the overall effect of that command will be to copy the highlighted line of text to the system clipboard.

Now the line of text will be in the system clipboard. So you should be able to paste it from the clipboard into any other external program that has access to the clipboard. Your browser, another text editor/IDE, etc.

NOTE: If you want to do a cut/paste operation, instead of copy/paste, you need to use the d command (delete) instead of y (yank).
In other words, select some text in one of vims visual selection modes (using V, v or c-v (where c- is vim-speak for the ctrl key)), then delete the text to the system clipboard using "+d.
This will copy the text to the clipboard and remove it from the file/buffer in vim. So this is equivalent to selecting text and using ctrl+x in windows (cut).

Again, once it’s in the clipboard, you can use an external program’s paste command to paste the text from the system clipboard.

Incidentally- another useful register in Vim is the /dev/null/ register, which is the _ register. This comes in handy if you want to delete sensitive information from a file and you don’t want it being stored in vims registers/history.
So for example, if you’re deleting a password, or some sensitive contact information from a file and you don’t want that text lingering in any of vim’s default copy/paste registers, you’d delete it to the /dev/null register.

The process is exactly the same as the "cut" or “delete to the system clipboard” that we did before, but this time we use the /dev/null register _ instead of the system clipboard register +.

So with a line of sensitive text selected, we’d use "_d to delete the text to /dev/null, to avoid it ending up in vim’s internal copy/paste registers.


Going the other way, if you have copied some text into the clipboard from your browser, or another program and you want to paste it into vim, you need to paste it via vim’s special clipboard register +.

So the order of operations is:
  • Copy some text into the clipboard from your browser (or other external program)
  • Switch to vim
  • Use motion keys to move to the line where you want to paste the text to.
  • Type the command "+p, where "+ tells Vim to use the system clipboard and p is vim’s Paste command.
This will paste whatever text is currently in the clipboard into your current file in vim.
 
To follow on, in order to copy/paste an entire file to the clipboard in vim:
ggVG"+y should do the trick.
ggVG selects all lines in the document, then "+y will "yank"/copy the text to the clipboard.

And to paste the text into a completely different instance of vim, in another terminal, you’d use "+p.

These instructions will 100% work in vim in "nocompatible"/vim mode, but I’m unsure about "compatible" mode (which behaves exactly as classic vi did, without any of vims many extensions/improvements.)
I haven’t used Vim in "compatible" mode for a long time. But I’m fairly certain that the registers are available in vim’s classic vi mode.
 
Also be advised the CentOS 7 EOL is 30 June 2024 - it is coming sooner then later - be planning ahead
 

Members online


Latest posts

Top