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

Joined
Apr 16, 2023
Messages
172
Reaction score
20
Credits
1,677
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
 
I came across this thread due to having a similar issue. Using Slackware current, have used vi (assuming vi is an alias or symlink to vim) a lot in the past (but feeling kind of rusty nowadays). Tried the "+y approach to no avail -- nothing goes to the X-Windows clipboard; and once the command p is used within vi, it copies the content copied with "+y to the vim text being edited. So, I was wondering if there is any particular mode or configuration for vim that would make it work as it is supposed to.

Note: I am using vim on Xterm.

Thanks for any input.
 
I'm not real sure what you are trying to do here.

There is an application called "gpm", most distro's have it. It will let
you use a mouse to copy and paste in a console terminal, to and from a GUI.
 
I'm not real sure what you are trying to do here.

There is an application called "gpm", most distro's have it. It will let
you use a mouse to copy and paste in a console terminal, to and from a GUI.
The gpm daemon works in the virtual consoles so that copying and pasting text in that environment works if gpm is installed. Unfortunately, what's copied into the selection buffer of gpm doesn't normally paste to an X environment which has it's own copy and paste buffer.

Both systems, gpm and the X selection buffers work in the same way with a three button mouse, highlighting with the left button and pasting with the middle button, but they copy to separate buffers which apply to their respective environments.

Using a file is a common way of carrying text from a virtual console screen to a GUI environment, though it's more involving than mouse usage.
 
Last edited:
I came across this thread due to having a similar issue. Using Slackware current, have used vi (assuming vi is an alias or symlink to vim) a lot in the past (but feeling kind of rusty nowadays). Tried the "+y approach to no avail -- nothing goes to the X-Windows clipboard; and once the command p is used within vi, it copies the content copied with "+y to the vim text being edited. So, I was wondering if there is any particular mode or configuration for vim that would make it work as it is supposed to.

Note: I am using vim on Xterm.

Thanks for any input.
I have no idea offhand. Are you using the full-fat version of vim? or is it something like vim-tiny?
Vim does have a lot of optional bits of functionality that can be included, or excluded from the build. So if you're using a build of vim that has limited functionality built-in, that would be why there's no clipboard support. Vi is often a shortcut to vim nowadays, usually provided via a package called something like vim-minimal, or vim-tiny - both of which have much less optional functionality built-in than the full version of vim. vi also sets the --compatible flag, which forces vim to act exactly like classic vi and disables all of vims built-in extensions. You could try running vi with the --no-compatible flag, which should re-enable any vim-extensions that are compiled into it.
So you could add set nocompatible to your .vimrc and vi should pick that up.

I think the first thing to do would be to check what options are compiled into the version of vi/vim that you are using. Clipboard support is part of the +X11 option. So run vim --version and see if the output shows +X11.

If it does, then support for the clipboard should be present.

If it doesn't, it means that X11 (and clipboard) support is NOT built into it, so you'd have to download and install a full-fat version of vim (if available), or you'd have to compile it from source yourself and enable whatever extra bits of functionality you need.

But if it IS compiled with +X11, then the "+ register should work. But you could also try the "* register. Because if memory serves, that also copies to the system clipboard.
I remember that "+ is the system clipboard selection register and "* is the primary selection register. I can't remember the exact differences between them. But I've always used the "+ register.
So perhaps try "*y to yank, or "*d to cut. Then try ctrl+v in a GUI editor, or your browser, to see if the "* register works for you.

Failing that, I'm out of ideas for now!
 
Last edited:
I have no idea offhand. Are you using the full-fat version of vim? or is it something like vim-tiny?
Vim does have a lot of optional bits of functionality that can be included, or excluded from the build. So if you're using a build of vim that has limited functionality built-in, that would be why there's no clipboard support. Vi is often a shortcut to vim nowadays, usually provided via a package called something like vim-minimal, or vim-tiny - both of which have much less optional functionality built-in than the full version of vim. vi also sets the --compatible flag, which forces vim to act exactly like classic vi and disables all of vims built-in extensions. You could try running vi with the --no-compatible flag, which should re-enable any vim-extensions that are compiled into it.
So you could add set nocompatible to your .vimrc and vi should pick that up.

I think the first thing to do would be to check what options are compiled into the version of vi/vim that you are using. Clipboard support is part of the +X11 option. So run vim --version and see if the output shows +X11.

If it does, then support for the clipboard should be present.

If it doesn't, it means that X11 (and clipboard) support is NOT built into it, so you'd have to download and install a full-fat version of vim (if available), or you'd have to compile it from source yourself and enable whatever extra bits of functionality you need.

But if it IS compiled with +X11, then the "+ register should work. But you could also try the "* register. Because if memory serves, that also copies to the system clipboard.
I remember that "+ is the system clipboard selection register and "* is the primary selection register. I can't remember the exact differences between them. But I've always used the "+ register.
So perhaps try "*y to yank, or "*d to cut. Then try ctrl+v in a GUI editor, or your browser, to see if the "* register works for you.

Failing that, I'm out of ideas for now!

Hello,

Thanks for your thorough response! Sorry for the long delay in following up.

Upon looking into the packages installed, it seems that the vim included within the distro is conventional/large one:



root@darkstar:/var/log# ls /var/log/packages/vim -l
-rw-r--r-- 1 root root 91568 Jan 8 2024 /var/log/packages/vim-9.1.0015-x86_64-1
-rw-r--r-- 1 root root 1740 Jan 8 2024 /var/log/packages/vim-gvim-9.1.0015-x86_64-1
root@darkstar:/var/log#

The option “nocompatible” did not make any difference in my case.

The end of the output of “vim –version” is likely a telltale of why that functionality is not working (as you pointed out):



bash-5.2$ vim --version | grep -i X11
+diff +modify_fname +sodium -X11

...

system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
defaults file: "$VIMRUNTIME/defaults.vim"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -fPIC -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -L/usr/local/lib -Wl,--as-needed -o vim -lm -ltinfo -lelf
-lsodium -lacl -lattr -lgpm -L/usr/lib64 -ltclstub8.6 -ldl -lz -lpthread -lm
root@darkstar:/var/log#


So, since the “-X11” is part of the output above (instead of “+X11”), it indicates that this functionality (vim using X11 clipboard as one of its buffers) simply is not built in the vim that comes along with my distro (which is kind of odd).

Thanks for all your insights!
 



Top