Can I copy/yank into and out of vim?

39 views
Skip to first unread message

K otgc

unread,
Mar 3, 2023, 7:08:50 PM3/3/23
to vim_use
Hello, I would like to copy/yank text from vim out to other programs like dpaste or Google Docs.
I would also like to coy/yank text from external programs like website text into vim.
I can manually highlight external program text with my mouse, copy and then insert into vim, but maybe there's a vim script for this?

Thank you :-)

c.willis111

unread,
Mar 3, 2023, 8:19:44 PM3/3/23
to vim...@googlegroups.com
Try
:help clip

regards - Chris

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/e896a120-f1fd-40db-b678-ec84a1fe6a8cn%40googlegroups.com.

Tim Chase

unread,
Mar 3, 2023, 8:29:37 PM3/3/23
to vim...@googlegroups.com
On 2023-03-03 16:08, K otgc wrote:
> Hello, I would like to copy/yank text from vim out to other programs
> like dpaste or Google Docs.

How you go about this depends on a couple factors that you omit:

- which OS are you using?

- do you mean via the clipboard (in which case, if your build was
built with clipboard support, you can use the "*" or "+" register
which are the same thing on Windows & MacOS, but are different on
X) or do you mean operating via some interface/API? And if you
intend to use the system clipboard, does your vim have clipboard
support built in? (look for "+clipboard" or "+xterm_clipboard" in
the output of ":version")

- are you only doing linewise selections in vim, or are you trying
to deal with characterwise and blockwise visual selections, too?

> I would also like to coy/yank text from external programs like website
> text into vim.

Solutions here would require answers to the above questions, too

> I can manually highlight external program text with my mouse, copy and
> then insert into vim, but maybe there's a vim script for this?

If using the clipboard register doesn't suffice, you might have to
detail whether you mean the Selection mode or Visual mode. And
possibly include your 'mouse' settings:

:set mouse?

(to help determine whether the selection is being handled by your
terminal or by vim)

-tim






akspecs

unread,
Mar 3, 2023, 9:19:37 PM3/3/23
to vim...@googlegroups.com


> On Mar 3, 2023, at 16:08, K otgc <kontheg...@gmail.com> wrote:
>
> Hello, I would like to copy/yank text from vim out to other programs […trimmed]

assuming your system’s install was compiled with the clipboard option, you can yank into the system’s “ register.

Angel M Alganza

unread,
Mar 3, 2023, 9:32:55 PM3/3/23
to vim...@googlegroups.com
I use Shift+Insert (in insert mode in Vim) to do just that. As far as I
can tell it works everywhere, although what's inserted in xterm and the
rest of X programs is different. Probably Shift+Inserts uses a different
"clipboard" for xterm and for everything else in X.

Tony Mechelynck

unread,
Mar 3, 2023, 9:33:26 PM3/3/23
to vim...@googlegroups.com
No script is necessary: yanking or deleting into the + register copies
or cuts, respectively, to the clipboard, and yanking from the +
register pastes from the clipboard. In gvim you can also use the menus
Edit→Cut, Edit→Copy and Edit→Paste just like in any other GUI program.

Of course for it to work you need a Vim compiled with +clipboard and
you need to be running it either as gvim or in a terminal with access
to the clipboard (not in, for instance, a Linux text console).

Best regards,
Tony.

Tony Mechelynck

unread,
Mar 3, 2023, 9:48:37 PM3/3/23
to vim...@googlegroups.com
The reason for this dfference in behaviour is that xterm doesn't use
the X11 clipboard (register + in Vim) but only the X11 selection
(register * in Vim). In almost every other GUI, including gvim, you
can use Edit→Cut, Edit→Copy and Edit→Paste to pass text from one
program to another via the clipboard, but in xterm you need to have
some text selected (and, in Vim, written to "*) then the middle mouse
button pastes into xterm; conversely, IIUC anything selected in xterm
will appear in Vim's * register and can be pasted with "*p or with
<MiddleMouse>.

Best regards,
Tony.

Angel M Alganza

unread,
Mar 3, 2023, 10:24:21 PM3/3/23
to vim...@googlegroups.com
On 2023-03-04 03:48, Tony Mechelynck wrote:

> The reason for this dfference in behaviour is that xterm doesn't use
> the X11 clipboard (register + in Vim) but only the X11 selection
> (register * in Vim). In almost every other GUI, including gvim, you
> can use Edit→Cut, Edit→Copy and Edit→Paste to pass text from one
> program to another via the clipboard, but in xterm you need to have
> some text selected (and, in Vim, written to "*) then the middle mouse
> button pastes into xterm; conversely, IIUC anything selected in xterm
> will appear in Vim's * register and can be pasted with "*p or with
> <MiddleMouse>.

Thank you, Tony. I don't know how many times I've read a similar
explanation over the almost 30 years I've been using Unix-like OS, but
for sure I don't have enough fingers to count them. Still, the only
thing I can understand is that there are different "clipboards" and that
Shift+Insert works sometimes but not always. Usually by trying several
times I end up getting "pasted" what I expect, and at times I need to
reach for a mouse (I hace mice unless they have fur and teeth) middle
button which also works some times. LOL

I don't seem to find an explanation of the different between what you
describe as "in xterm you need to have some text selected" and whatever
the alternative (not having some text selected?) might be. :-)

Cheers,
Ángel

Gary Johnson

unread,
Mar 4, 2023, 1:12:54 AM3/4/23
to vim...@googlegroups.com
This has always confused me, so I've taken to using autocutsel to
copy the selection to the clipboard and vice versa whenever either
is updated. I have a script that runs these commands at startup:

autocutsel -fork
autocutsel -selection PRIMARY -fork

There's a good article on this here:
http://mutelight.org/articles/subtleties-of-the-x-clipboard.

I also use this setting in my vimrc:

set clipboard^=unnamed

so that I can copy and paste everything through the default unnamed
register.

The resulting behavior of having almost every cut or copy operation
overwriting the clipboard will drive some people crazy, but it works
well for me.

Regards,
Gary

Tony Mechelynck

unread,
Mar 5, 2023, 2:59:18 AM3/5/23
to vim...@googlegroups.com
After doing Edit→Cut in some program (including gvim, where "+d is
also available), the text you cut is no longer selected, but it is in
the clipboard. Using Edit→Paste elsewhere (in the same program or
another one) will paste it, but not into xterm, which doesn't use the
clipboard but only the selection. In xterm there are no Edit→Copy,
Edit→Cut or Edit→Paste menus, nor do Ctrl-C, Ctrl-X or Ctrl-V (or some
other keyboard mechanism such as Vim's "+y "+d "+p "+P sequences)
exist in xterm.

jr

unread,
Mar 5, 2023, 7:35:03 AM3/5/23
to vim...@googlegroups.com
On Sun, 5 Mar 2023 at 07:59, Tony Mechelynck
<antoine.m...@gmail.com> wrote:
>
> On Sat, Mar 4, 2023 at 4:24 AM Angel M Alganza <a...@ugr.es> wrote:
> >
> > On 2023-03-04 03:48, Tony Mechelynck wrote:
> >
> > > The reason for this dfference in behaviour is that xterm doesn't use
> > > the X11 clipboard (register + in Vim) but only the X11 selection ...
> > ...
> > I don't seem to find an explanation of the different between what you
> > describe as "in xterm you need to have some text selected" and whatever
> > the alternative (not having some text selected?) might be. :-)

man 1 xterm. man 1 xclipboard too is useful.

--
regards, jr.

Rory Campbell-Lange

unread,
Mar 6, 2023, 4:50:38 PM3/6/23
to vim...@googlegroups.com
On my urxvt console I seem to be able to copy and paste with control-alt-c and
control-alt-p. Otherwise the middle button works pretty well for pasting
console selected text.

Rory

Rory Campbell-Lange

unread,
Mar 6, 2023, 5:12:25 PM3/6/23
to vim...@googlegroups.com
Another way with pipes (on a linux terminal, at least) is

read selection from system clipboard
:r !xclip -o

write selection to system clipboard but read it back to keep it
:'<,'>! xclip -in && xclip -o

Rory

Igor Lerinc

unread,
Mar 7, 2023, 1:47:47 AM3/7/23
to vim_use
""noremap <Leader>y "*y
""noremap <Leader>p "*p
"
noremap <Leader>y "+y
noremap <Leader>p "+p
noremap <Leader>P "+P


this works for me, on windows and linux both

meine

unread,
Mar 11, 2023, 3:59:01 AM3/11/23
to vim...@googlegroups.com
On Sun, Mar 05, 2023 at 08:58:59AM +0100, Tony Mechelynck wrote:
> On Sat, Mar 4, 2023 at 4:24 AM Angel M Alganza <a...@ugr.es> wrote:
> >
> > On 2023-03-04 03:48, Tony Mechelynck wrote:
> >
> > > The reason for this dfference in behaviour is that xterm doesn't use
> > > the X11 clipboard (register + in Vim) but only the X11 selection
> > > (register * in Vim). In almost every other GUI, including gvim, you
> > > can use Edit→Cut, Edit→Copy and Edit→Paste to pass text from one
> > > program to another via the clipboard, but in xterm you need to have
> > > some text selected (and, in Vim, written to "*) then the middle mouse
> > > button pastes into xterm; conversely, IIUC anything selected in xterm
> > > will appear in Vim's * register and can be pasted with "*p or with
> > > <MiddleMouse>.

To add confusion:

The Fish shell accepts Ctrl-V as a paste command, as well as Shift-Ins
from the keyboard.

This works in Xterm and urxvt.

In plain Xterm with the sh shell, only Shift-Ins works as paste.

While testing and writing this down I found out that there are a lot of
differences that seem to be dependent on de combination of your terminal
multiplexer, your shell, and Vim. Finding the proper combinations,
the output of `:registers` might be helpful to compare.

//meine

meine

unread,
Mar 11, 2023, 4:12:33 AM3/11/23
to vim...@googlegroups.com
> To add confusion:

addendum: where the outside world adapted on the same set of Ctrl-
commands to move text, Vim uses <C-v> for a visual block, and <C-x> for
autocompletion. It is alike, but not the same.

//meine
Reply all
Reply to author
Forward
0 new messages