Reputation: 35485
Is it possible to copy to clipboard directly from Vim? yy
only copies stuff to Vim's internal buffer. I want to copy to the OS's clipboard. Is there any such command in Vim or you can only yank stuff within Vim?
Upvotes: 1171
Views: 1073954
Reputation: 121
This is an answer for those who do not use X11, and want to customise vim to use a custom copy/paste command. For instance, I typically use vim from a virtual machine that I SSH in to from my mac, thus I cannot use vim X11 clipboard support or GUI clipboard support.
My task is to mirror the clipboard from macOS and the vim unnamed register ("
) via your custom commands for clipboard copying and pasting.
For example, for my SSH setup, I use these command as copy things from my VM to mac and listen to the mac clipboard from my VM:
copy-from-vm.sh
echo -n "$1" | ssh -T -i ~/mac.pem username@my-mac LANG=en_US.UTF-8 pbcopy
listen-from-vm.sh
(with a copy of klipsustreamer on my mac)
ssh -n -tt -q -i ~/mac.pem username@my-mac /path/to/klipsustreamer 2> /dev/null
Once you've got working commands to paste and listen to the clipboard for your custom situation, put those commands in g:manualclipcopycmd
and g:manualclippastelisten
and add the following to your vimrc
.
let g:manualclipcopycmd = '/path/to/copy-from-vm.sh'
let g:manualclippastelisten = [ 'bash', '-c', '/home/user/clipboard-listen']
" since I do not use X11, my `*` and `+` registers are mapped by vim
" to my unnamed register, and I've specified that explicitly
set clipboard=unnamed
function! Manualcopy(event)
let regcontents = a:event['regcontents']
" you can limit the registers to those interesting to you if desired
" we use job_start instead of system to not block
" in case copying to clipboard takes time
call job_start ([ g:manualclipcopycmd, join (regcontents, "\n") ])
endfunction
function! Manualpaste(channel, str)
call setreg ("", json_decode (a:str).data)
endfunction
let g:cliplistenjob = job_start(g:manualclippastelisten, { 'callback': 'Manualpaste' })
augroup manualclipboard
autocmd!
autocmd TextYankPost * call Manualcopy(v:event)
augroup end
Upvotes: 0
Reputation: 23
Use WAYLAND
sudo apt install wl-clipboard # Debian
And in VIM
:w !wl-copy
To copy all just press gg then V then G and then execute the command
Upvotes: 0
Reputation: 41
Maybe someone will find it useful. I wanted to stay independent from X clipboard, and still be able to copy and paste some text between two running vims.
This little code save the selected text in temp.txt file for copying. Put the code below into your .vimrc
.
Use CTRL-c CTRL-v
to do the job.
vnoremap <C-c> :w !cp /dev/null ~/temp.txt && cat > ~/temp.txt<CR><CR>
noremap <C-v> :r !cat ~/temp.txt<CR>
Upvotes: 4
Reputation: 384394
Keybindings to make it easier
I have been using these for many years now:
nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$
You can now just use upper case Y
to copy to clipboard, and lowercase y
won't be affected e.g. as by set clipboard=unnamed
so you can still choose if the copy will go to the clipboard or not.
Tested on ubuntu 21.04, vim 8.2.
Upvotes: 26
Reputation: 11024
Besides vim-gnome, "+y is also supported by default in neovim on Ubuntu 20.04.
If you don't want to install a new program, you could always do the lazy method of cat file.txt
or gedit file.txt
and copy from there.
Upvotes: 2
Reputation: 1490
If you are using vim in and old version of macOS, unfortunately the shipped version of vim may and not be compiled with clipboard options. Luckily, homebrew can easily solve this problem.
Install vim:
brew install vim
Install gui verion of vim:
brew install macvim
Restart the terminal to take effect.
Append the following line to ~/.vimrc
set clipboard=unnamed
Now you can copy the line in vim with yy
and paste it system-wide.
Updated Method:
I was never satisfied with set clipboard method for years. The biggest drawback is it will mess up your clipboard history, even when you use x
for deletion. Here is a better and more elegant solution.
Copy the text [range] of vim into the system clipboard. (Hint: use v
or V
to select the range first, and then type the colon :
to activate the Ex command):
:[line-range]yank +
E.g., to copy/yank lines 5-10 to the system clipboard * register use:
:5,10y *
Paste the content from the system clipboard into vim on a new line:
:put +
Note:
"*y
or "+y
to save this visual block to clipboard. However this is hard to type, I am still thinking about alternatives.:help "*
or :help *+
for more informationsbrew info vim
will be able to see other options for installing vim. Currently it does not have any other options.Upvotes: 47
Reputation: 37145
For Ubuntu - July 2018
Use the register "+
to copy to the system clipboard (i.e. "+y
instead of y
).
Likewise you can paste from "+
to get text from the system clipboard (i.e. "+p
instead of p
).
You have to also make sure that vim is compiled with support for the clipboard. Try:
vim --version | grep .xterm_clipboard -o
and if it's -xterm_clipboard
(a minus prefix) then you do not have support.
Here are some instructions for swapping out with a working version of vim that has clipboard support.
$ sudo apt-get purge vim
$ sudo apt-get autoremove (removes any extraneous vim dependency packages from system)
$ sudo apt-get install vim-gnome (or `sudo apt-get install vim-gtk3` for newer Ubuntu versions)
Check again with vim --version | grep .xterm_clipboard -o
and you can confirm the clipboard is now available (ie. +xterm_clipboard
)
Upvotes: 69
Reputation: 947
This answer contains details specific to macOS users.
Append the following line to ~/.vimrc
:
set clipboard=unnamed
If this does not work, check if your installed version maybe has the clipboard
feature not enabled. When this answer was written (2019), the default vim shipped with macOS did not come with clipboard option enabled. You need that option to access the system clipboard.
To check if your vim has that option enabled use the below command
vim --version | grep clipboard
In the result, you should have +clipboard
. If it is -clipboard
, then your VIM does NOT have the option to access the system clipboard.
You need to MAKE and install your VIM with the option you need. Following are the commands.
# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"
The above will install the latest VIM with the option +clipboard
enabled.
Now you can yank text to system clipboard. Below steps explains how to yank.
press v
, this will switch you to VISUAL mode.Press y
, this will copy the selected text to clipboard.CMD + v
to paste.I use MACBook Pro with macOS Mojave and the above works in it.
Upvotes: 37
Reputation: 52608
In your vimrc file you can specify to automatically use the system clipboard for copy and paste.
On macOS and Windows set:
set clipboard=unnamed
On Linux set (vim 7.3.74+):
set clipboard=unnamedplus
NOTE: You may need to use an up to date version of Vim for these to work.
http://vim.wikia.com/wiki/Accessing_the_system_clipboard
Upvotes: 285
Reputation: 2375
For Mac OS X, in the terminal:
vim --version | grep clipboard
to check if the clipboard is enabled(with +)if has("clipboard")
set clipboard=unnamed " copy to the system clipboard
if has("unnamedplus") " X11 support
set clipboard+=unnamedplus
endif
endif
reference: Setting up Vim to yank to clipboard on Mac OS X
Upvotes: 0
Reputation: 2909
In linux with gnome (ubuntu, xubuntu etc) install vim-gnome and you will be able to use VISUAL to select and then Ctrl + C normally and paste in other applications with Ctrl + V
sudo apt install vim-gnome
Upvotes: 1
Reputation: 332
I saw many answers on this question and the way to make this work was a combination of many.
The steps I followed to make vim copy to system clipboard are
sudo apt remove vim
. (I was too lazy to find how to re-compile it with the +clipboard support.sudo apt install vim-athena
that ships with +clipboard.~/.vimrc
the following line: set clipboard=unnamedplus
.source %
.Note: I am using Ubuntu 20.04.
Upvotes: 2
Reputation: 411
I have struggled a lot in copying to clipboard. Inside Vim it is quite simple using visual mode but if you want to copy to the clipboard things are quite messsed. I have simple method of copying using xclip utility. For this you must have to install xclip first.
for the whole file it is very simple
xclip -sel clip filename
but if you want to copy only a particular range of line numbers
tail -n +[n1] filename | head -n [n2] | xclip -sel clip
you can make use of ~/.bashrc to simplify this
#rangecp copy lines from n1 to n2 from a given file to clipboard
function rangecp()
{
if [ -f $1 ]
then
if [ ! -z $3 ]
then
diff=$(($3 - $2 + 1))
#diff=`expr $3 - $2 + 1`
tail -n +$2 $1 | head -n $diff | xclip -sel clip
elif [ ! -z $2 ]
then
tail -n +$2 $1 | xclip -sel clip
else
echo "Provide a range from [n1] lines to [n2] lines"
fi
else
echo "[ $1 ] file doesn't exist"
fi
}
then
source ~/.bashrc
How to use
rangecp filename.txt 50 89
rangecp filename.txt 50
Upvotes: 1
Reputation: 8138
I want to supplement a way to copy the line to the clipboard and use it on the function.
here is an example that you open the URL in the browser
let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
" copy the line and put it in the register *
normal "*yy
:execute "silent !start ".g:chrome_exe." ".getreg("*")
endfunction
map ,url :call OpenURL()<CR>
Upvotes: -1
Reputation: 1184
This question already has a lot of answers. I am adding my way which I think is quick.
Quickly, you can press V
(Shift + v) to active visual mode. In visible mode, you can use j
and k
to select the text you want to copy. After selection, use
"*y
Now, selected text is copied to clipboard.
Upvotes: 28
Reputation: 153
I'm on mac osx (10.15.3) and new to vim. I found this so frustrating and all the answers on here too complicated and/or didn't apply to my situation. I ended up getting this working in 2 ways:
key mapping that uses pbcopy: works on the old version of vim that ships with mac.
Add
vmap '' :w !pbcopy<CR><CR>
to your ~/.vimrc
Now you can visually select and hit''
(two apostrophes) to copy to clipboard
Install newer version of vim so I can access the solution most recommended in other answers:
brew install vim
alias vim=/usr/local/bin/vim
(should add this to your ~/.bashrc or equivalent)
Now you can visually select and hit"+yy
to copy to clipboard
Upvotes: 4
Reputation: 32112
The *
register will do this. In Windows, +
and *
are equivalent. In unix there is a subtle difference between +
and *
:
Under Windows, the * and + registers are equivalent. For X11 systems, though, they differ. For X11 systems, * is the selection, and + is the cut buffer (like clipboard). http://vim.wikia.com/wiki/Accessing_the_system_clipboard
*
is probably what you want most of the time, so I use *
because it functions as I expect it to in both environments.
In Linux distros you have to install vim-gtk
(aka gvim
) first to gain clipboard functionality. This is because non-gtk vim is typically compiled without X11 support. This is to allow it to run on console only machines (often servers).
And for those confused about how to use registers when yanking or putting, you merely write "
then the name of the register. So for copying something to the clipboard register you type "*y
and then to put you type "*p
(credit: Kyle Mathews)
Upvotes: 708
Reputation: 107
My solution was putting the following line to .vimrc:
map <C-y> :w !xclip -sel c <CR><CR>
The script copies the selected line (trough visual mode) or the file content (if none is selected) to the clipboard using Ctrl + y. I'm using Manjaro Linux if that matters.
Upvotes: 8
Reputation: 348
In my instance selecting text by highlighting (clicking and dragging) with my mouse was causing vim to enter into visual mode.
On a mac anyway, the easiest solution is to use fn + mouse click and drag
to avoid entering into visual mode.
If you wish to avoid this behavior by default you can edit your vimrc
Upvotes: 1
Reputation: 734
I've been struggling with this for months till now on MacOsX using keyboard shortcuts. I know question isn't about using keyboard shorts. But this might help someone with the same concern.
I found that if you uncheck:
View -> Allow Mouse Reporting
from Terminal menu, you'll be able to copy to clipboard using
command + c
again.
Upvotes: 9
Reputation: 46479
Shift+Ctrl+C if you are in graphical mode of Linux, but first you need to select what you need to copy.
Upvotes: -2
Reputation: 187
If you have xclip an easy way of copying text to the clipboard is as follows:
y
command in vanilla vim):call system("xclip -selection clipboard", @")
:call system()
runs a terminal command. It takes two arguments, the first the command, the second what to pipe to that command. For example :echom system("head -1", "Hello\nWorld")
returns Hello (With some padding). echom returns the output of a command, call doesn't.
xclip -selection clipboard
just copies text into the system clipboard as opposed to the default X clipboard, (Accessed by the middle moue button).
@"
returns the last yanked text. " is the default register, but you could use any register. To see the contents of all registers, type :registers
.
Upvotes: 14
Reputation: 1042
I had issue because my vim was not supporting clipboard:
vim --version | grep clip
-clipboard +insert_expand +path_extra +user_commands
+emacs_tags -mouseshape +startuptime -xterm_clipboard
I installed vim-gnome (which support clipboard) and then checked again:
vim --version | grep clipboard
+clipboard +insert_expand +path_extra +user_commands
+emacs_tags +mouseshape +startuptime +xterm_clipboard
Now I am able to copy and paste using "+y and "+p respectively.
Upvotes: 3
Reputation: 89
I'm a Vim newby but, to copy all the text to system clipboard (e.g if you want to paste it to a word processor or another text editor like gedit, mousepad etc...), in normal mode:
ggVGy
or, more simply:
:%y
As suggested, I also installed vim-gtk and put
set clipboard=unnamedplus
in my .vimrc
and everything works fine
If you want to copy only a portion of text, use visual mode (v), select the text you want to copy and press y.
Finally, I suggest a clipboard program like Clipman (my favorite), Clipit, Parcellite or similar.
(I'm using vim 8.0 in Debian Stretch Xfce)
FORGIVE ME FOR MY ENGLISH! :-)
Upvotes: 8
Reputation: 23558
for OSX, like the 10342 answers above made clear, you need to make sure that vim supports the clipboard feature, said the the one that comes pre-shipped with OSX does NOT support clipboard, and that if you run
brew install vim
it would work.
Except that running vi will still make you run the preshipped OSX version, not the one you installed from brew.
to get over this, I simply aliased my vim command to the brew version, not the OSX default one:
alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/vim"
and now i'm golden
Upvotes: 1
Reputation: 193
You can find the answers here Arch Wiki
For Linux:
First of all you have to enable Clipboard in your vim version by installing
gvim
.
Next you have to put this line on your .vimrc
file.
set clipboard=unnamedplus
Upvotes: 18
Reputation: 17980
Nothing above worked for me on my windows laptop.
Ctrl+C was fine for copying.. but I needed Shift+Insert to Paste !
(A good reason to always get a laptop where Insert can always be accessed without pressing a secondary key)
Upvotes: 0
Reputation: 23855
selecting
with the the help of the mouse
and right-click copy
worked in my case.
I didn't want the line numbers included so I :set nonumber
before copying
.
Upvotes: 0
Reputation: 3376
In vim under ubuntu terminal only,
press shift + drag mouse
to select a text in vim then ctrl + shift + c
on the terminal
then ctrl + v
on other editor
Upvotes: 38
Reputation: 31302
On Mac OSX
copy selected part: visually select text(type v
or V
in normal
mode) and type :w !pbcopy
copy the whole file :%w !pbcopy
paste from the clipboard :r !pbpaste
On most Linux Distros, you can substitute:
pbcopy
above with xclip -i -sel c
or xsel -i -b
pbpaste
using xclip -o -sel -c
or xsel -o -b
xsel
and xclip
) are preinstalled on your distro, you can probably find them in the reposUpvotes: 492