gvim auto copy selected text to clipboard

575 views
Skip to first unread message

Sergey Vakulenko

unread,
Apr 20, 2012, 7:56:15 AM4/20/12
to vim_use
Hi

I wonder, how i can enable auto copy of selected text into '+'
register in Ubuntu (to share clipboard between apps)?
i have set guioptions+=a (in win32, this works perfectly )

Please do not offer hand-click solutions like vmap <C-Insert> "+y

Marcin Szamotulski

unread,
Apr 20, 2012, 1:26:11 PM4/20/12
to vim...@googlegroups.com
> --
> 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 can add unnamedplus to your 'clipboard' setting. See ":help 'cb'".
For example I use:
set clipboard=unnamedplus,unnamed,autoselect,exclude:cons|linux

Best regards,
Marcin Szamotulski

Sergey Vakulenko

unread,
Apr 20, 2012, 4:38:22 PM4/20/12
to vim_use
Hi Marcin

I tried, but this not works for me :(

set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux


test case:
1) open gvim
2)shift-v, move cursor and Esc (select lines in visual mode)
3)go to firefox and click ctrl-v or ctrl-Insert

here is what my gvim version show (+X11 +xterm_clipboard are onboard):

VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 6 2011 10:19:44)
Included patches: 1-154
Modified by pkg-vim-m...@lists.alioth.debian.org
Compiled by buildd@
Huge version with GTK2 GUI. Features included (+) or not (-):
+arabic +autocmd +balloon_eval +browse ++builtin_terms +byte_offset
+cindent +clientserver +clipboard +cmdline_compl +cmdline_hist
+cmdline_info +comments +conceal +cryptv +cscope
+cursorbind +cursorshape +dialog_con_gui +diff +digraphs +dnd -ebcdic
+emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path
+find_in_path +float +folding -footer +fork()
+gettext -hangul_input +iconv +insert_expand +jumplist +keymap
+langmap +libcall +linebreak +lispindent +listcmds +localmap +lua
+menu +mksession +modify_fname +mouse +mouseshape
+mouse_dec +mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -
osfiletype +path_extra +perl +persistent_undo
+postscript +printer +profile +python -python3 +quickfix +reltime
+rightleft +ruby +scrollbind +signs +smartindent -sniff +startuptime
+statusline -sun_workshop +syntax +tag_binary
+tag_old_static -tag_any_white +tcl +terminfo +termresponse
+textobjects +title +toolbar +user_commands +vertsplit +virtualedit
+visual +visualextra +viminfo +vreplace +wildignore
+wildmenu +windows +writebackup +X11 -xfontset +xim +xsmp_interact
+xterm_clipboard -xterm_save




On Apr 20, 7:26 pm, Marcin Szamotulski <msza...@gmail.com> wrote:
> On 04:56 Fri 20 Apr     , Sergey Vakulenko wrote:
>
> > Hi
>
> > I wonder, how i can enable auto copy of selected text into '+'
> > register in Ubuntu (to share clipboard between apps)?
> > i have  set guioptions+=a        (in win32, this works perfectly )
>
> > Please do not offer hand-click solutions like vmap <C-Insert> "+y
>
> > --
> > 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, visithttp://www.vim.org/maillist.php

Ben Fritz

unread,
Apr 20, 2012, 4:50:15 PM4/20/12
to vim...@googlegroups.com
On Friday, April 20, 2012 3:38:22 PM UTC-5, Sergey Vakulenko wrote:
> Hi Marcin
>
> I tried, but this not works for me :(
>
> set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux
>
>
> test case:
> 1) open gvim
> 2)shift-v, move cursor and Esc (select lines in visual mode)
> 3)go to firefox and click ctrl-v or ctrl-Insert
>

Normally the selection register is pasted with a middle-click of the mouse, not with CTRL-V. 'clipboard' containing autoselect should allow this. I have no idea whether it is possible to paste the selection with CTRL-V, but I don't think it is Vim which controls this.

flebber

unread,
Apr 21, 2012, 3:30:59 AM4/21/12
to vim_use
Well if you pasted it correctly you used a forward slash and not a
pipe.
> set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux

Should be |linux

and ubuntu should be CTRL+SHIFT+V usually.

Sayth

Christian Brabandt

unread,
Apr 21, 2012, 4:17:37 AM4/21/12
to vim_use
Hi flebber!

On Sa, 21 Apr 2012, flebber wrote:

> > set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux
>
> Should be |linux

No it shouldn't, since this is used as a pattern.

> and ubuntu should be CTRL+SHIFT+V usually.

depends on the desktop environment / window manager.

regards,
Christian
--

Sergey Vakulenko

unread,
Apr 21, 2012, 9:21:44 AM4/21/12
to vim_use

>Normally the selection register is pasted with a middle-click of the mouse, not with CTRL-V. 'clipboard' containing autoselect should allow
>this. I have no idea whether it is possible to paste the selection with CTRL-V, but I don't think it is Vim which controls this.

Im not use mouse for copy paste. Also, i used this feature (copy on
select) on Win XP 4 years. I cant understand how i cant do the same in
Ubuntu.

Please, help me!

Christian Brabandt

unread,
Apr 21, 2012, 9:36:51 AM4/21/12
to vim_use
Hi Sergey!

On Sa, 21 Apr 2012, Sergey Vakulenko wrote:

> >Normally the selection register is pasted with a middle-click of the mouse, not with CTRL-V. 'clipboard' containing autoselect should allow
> >this. I have no idea whether it is possible to paste the selection with CTRL-V, but I don't think it is Vim which controls this.
>
> Im not use mouse for copy paste. Also, i used this feature (copy on
> select) on Win XP 4 years. I cant understand how i cant do the same in
> Ubuntu.

Either yank it using y and then Ctrl-V should simply work, or use a
clipboard manager like parcellite, that syncs primary selection and the
clipboard.

regards,
Christian

--
Wer lange genug gelebt hat, hat alles gesehen und auch das Gegenteil
von allem.
-- Charles Maurice de Talleyrand

Gary Johnson

unread,
Apr 21, 2012, 6:26:27 PM4/21/12
to vim_use
On 2012-04-21, Sergey Vakulenko wrote:
> >Normally the selection register is pasted with a middle-click of the mouse, not with CTRL-V. 'clipboard' containing autoselect should allow
> >this. I have no idea whether it is possible to paste the selection with CTRL-V, but I don't think it is Vim which controls this.
>
> Im not use mouse for copy paste. Also, i used this feature (copy on
> select) on Win XP 4 years. I cant understand how i cant do the same in
> Ubuntu.

Windows and X use different models for sharing a "clipboard" between
applications. There is only only Windows clipboard but there are
two X mechanisms: the selection and the clipboard. See :help
x11-selection" for more on this. This can be confusing because when
you "copy" in X, sometimes the copied text goes to the selection and
sometimes it goes to the clipboard. Similarly, pasting may use
either of these as the source. I'm sure it actually works very
consistently, but I've never completely understood it.

Here's what I have in my ~/.vimrc:

if has("clipboard")
set clipboard^=unnamed " The "exclude:" entry, which is present by
" by default, must be last. Therefore, put
" "unnamed" first.
endif

With that, I am able to easily yank and paste between applications
including vim and gvim. However, to paste into another X
application such as Firefox, I must use the mouse middle button.
Ctrl-V seems to use the other selection mechanism.

I tried changing "unnamed" to "unnamedplus" in the 'clipboard'
setting and was able to use Ctrl-V for pasting in Firefox, but then
was unable to use other mechanisms I was used to for copying and
pasting, so I went back to "unnamed". I didn't investigate the
behaviors thoroughly.

So I think you _can_ get copying and pasting to work well for you in
Ubuntu, but it may not work the same as it did in Windows.

Regards,
Gary

Marcin Szamotulski

unread,
Apr 22, 2012, 8:09:22 AM4/22/12
to vim...@googlegroups.com

If you add both of them (i.e. unnamed and unnamedplus) you will get what you
want as I already pointed.

Best regards,
Marcin

Gary Johnson

unread,
Apr 22, 2012, 2:11:47 PM4/22/12
to vim...@googlegroups.com, Marcin Szamotulski

I thought I also tried that when you first posted it. I thought I
set both, yanked text from one vim instance with 'Y', then tried to
put it into another vim instance with ":put". The ":put" text was
not what I had just yanked but was from the other selection. I just
tried it again after reading your latest reply and it seems to work.
I was in a hurry and must have done something wrong the first time.
I'll try it this week at work where it will get more of a workout.

Thanks for the tip and for insisting that it worked.

Regards,
Gary

BPJ

unread,
Apr 22, 2012, 4:37:22 PM4/22/12
to vim...@googlegroups.com
On 2012-04-22 20:11, Gary Johnson wrote:
>> If you add both of them (i.e. unnamed and unnamedplus) you will get what you
>> > want as I already pointed.
> I thought I also tried that when you first posted it. I thought I
> set both, yanked text from one vim instance with 'Y', then tried to
> put it into another vim instance with ":put". The ":put" text was
> not what I had just yanked but was from the other selection. I just
> tried it again after reading your latest reply and it seems to work.

Perhaps the order in which you add them matters?

/bpj

Chris Jones

unread,
Apr 22, 2012, 11:17:23 PM4/22/12
to vim...@googlegroups.com, Marcin Szamotulski
On Sun, Apr 22, 2012 at 02:11:47PM EDT, Gary Johnson wrote:

[..

> > If you add both of them (i.e. unnamed and unnamedplus) you will get
> > what you want as I already pointed.

> I thought I also tried that when you first posted it.

> I thought I set both, yanked text from one vim instance with 'Y', then

> tried [..]

Hello Gary,

That's a lot of ‘thinking’, don't you think..: no offense meant.. :-)

The ‘:registers’ command is the healthier methodology when monitoring
cut's, copy's & paste's.

Whatever I do, mouse or nomouse, keyboard, joystick... -) .. the ‘:reg’
command lets me see exactly what is going on.. And if I'm not sure I get
to :reg a second time, even a third time for free..

I used to have problems with the different copypaste scenarios, but now
that I use ‘:reg’ on a regular basis, I always seem to end up with the
copied stuff available somewhere or other.

Seeing is believing.

CJ

--
WHAT YOU SAY??

Gary Johnson

unread,
Apr 23, 2012, 2:57:58 AM4/23/12
to vim...@googlegroups.com
On 2012-04-22, Chris Jones wrote:
> On Sun, Apr 22, 2012 at 02:11:47PM EDT, Gary Johnson wrote:
>
> [..
>
> > > If you add both of them (i.e. unnamed and unnamedplus) you will get
> > > what you want as I already pointed.
>
> > I thought I also tried that when you first posted it.
>
> > I thought I set both, yanked text from one vim instance with 'Y', then
> > tried [..]
>
> Hello Gary,
>
> That's a lot of ‘thinking’, don't you think..: no offense meant.. :-)

Hi Chris,

What are you, my English teacher? It's probably a defensive habit
to give myself wiggle room. (Note the careful use of "probably"
instead of "think".) You want to be my shrink, too? No offense
taken. And none intended, honestly.

> The ‘:registers’ command is the healthier methodology when monitoring
> cut's, copy's & paste's.
>
> Whatever I do, mouse or nomouse, keyboard, joystick... -) .. the ‘:reg’
> command lets me see exactly what is going on.. And if I'm not sure I get
> to :reg a second time, even a third time for free..
>
> I used to have problems with the different copypaste scenarios, but now
> that I use ‘:reg’ on a regular basis, I always seem to end up with the
> copied stuff available somewhere or other.

Good idea. With that, I did some experimentation.

Case 1:
clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux

Copy text from a GNOME Terminal by dragging the mouse over it
with the left button held down.

":registers" shows selected text in the * register. The
contents of the + and " registers are unchanged from before the
copy operation.

":put" pastes the contents of the + register, not what I wanted.

Case 2:
clipboard=unnamed,unnamedplus,autoselect,exclude:cons\|linux

Copy text from a GNOME Terminal as before. ":registers" shows
the same results as before, as expected.

":put" again pastes the contents of the + register.

Case 3:
clipboard=unnamed,autoselect,exclude:cons\|linux

Copy text from a GNOME Terminal as before. ":registers" shows
the same results as before, as expected.

":put" now pastes the contents of the * register.

So it appears that the only way to paste from the * register without
explicitly naming it is to exclude "unnamedplus" from the
'clipboard', and that means that unnamed yanks from Vim go only to
the * register and not the + register, which then means that Ctrl-V
in Firefox won't paste that yanked text. That's really fine with me
since I paste into X applications other than Vim with the middle
mouse button, not Ctrl-V, but it's not what the OP wanted.

By naming registers, you can copy and paste any way you like in Vim,
but I don't see a way to copy from Vim to another X application, or
from another X application to Vim, without having to know about both
the + and * registers. It would be nice if there was some way to
avoid all that thinking.

Regards,
Gary

Chris Jones

unread,
Apr 23, 2012, 7:26:55 AM4/23/12
to vim...@googlegroups.com
On Mon, Apr 23, 2012 at 02:57:58AM EDT, Gary Johnson wrote:
> On 2012-04-22, Chris Jones wrote:
> > On Sun, Apr 22, 2012 at 02:11:47PM EDT, Gary Johnson wrote:

[..]

> What are you, my English teacher? It's probably a defensive habit to
> give myself wiggle room. (Note the careful use of "probably" instead
> of "think".) You want to be my shrink, too? No offense taken. And
> none intended, honestly.

:-)

Forgive me... was in a ‘funny’ mood last night.. pixilated, perhaps..

> > The ‘:registers’ command is the healthier methodology when
> > monitoring cut's, copy's & paste's.

> Good idea. With that, I did some experimentation.

That was supposed to be the useful part.

The idea being that part of the problem is often in the mind, rather
than actual facts. I'm not claiming that once I started using the :reg
command to check the contents of Vim's registers on a regular basis all
the problems just went away, but at least those problems that remained
were genuine, not something I was making up.

> Case 1:
> clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux
>
> Copy text from a GNOME Terminal by dragging the mouse over it
> with the left button held down.
>
> ":registers" shows selected text in the * register. The
> contents of the + and " registers are unchanged from before the
> copy operation.
>
> ":put" pastes the contents of the + register, not what I wanted.
>
> Case 2:
> clipboard=unnamed,unnamedplus,autoselect,exclude:cons\|linux
>
> Copy text from a GNOME Terminal as before. ":registers" shows
> the same results as before, as expected.
>
> ":put" again pastes the contents of the + register.
>
> Case 3:
> clipboard=unnamed,autoselect,exclude:cons\|linux
>
> Copy text from a GNOME Terminal as before. ":registers" shows
> the same results as before, as expected.
>
> ":put" now pastes the contents of the * register.

I'm still running Vim 7.2, so I have not had the opportunity to use
unnamedplus as yet. My experience is limited to Case 3.

> So it appears that the only way to paste from the * register without
> explicitly naming it is to exclude "unnamedplus" from the 'clipboard',
> and that means that unnamed yanks from Vim go only to the * register
> and not the + register, which then means that Ctrl-V in Firefox won't
> paste that yanked text. That's really fine with me since I paste into
> X applications other than Vim with the middle mouse button, not
> Ctrl-V, but it's not what the OP wanted.

That's also what I am seeing (Case 3). But I run Vim under GNU/screen
which I have customized to write to what Vim sees as the + register,
X11's PRIMARY selection, IIRC.

From a more general perspective, the OP could use the ‘autocutsel’
program that keeps X11's clipboard & primary selection in sync'.

> By naming registers, you can copy and paste any way you like in Vim,
> but I don't see a way to copy from Vim to another X application, or
> from another X application to Vim, without having to know about both
> the + and * registers. It would be nice if there was some way to
> avoid all that thinking.

Another approach would be to figure out if there's any way GTK apps can
be made to always use either the clipboard ("*) or X11's PRIMARY ("+)
rather than a mix of both depending on what copy/paste mechanisme you
are using.

Best,

CJ

--
SOMEBODY SET UP US THE BOMB

Christian Brabandt

unread,
Apr 23, 2012, 5:12:14 PM4/23/12
to vim...@googlegroups.com, vim-dev Mailingliste
Hi Gary!

That should probably be documented. Here is a doc patch:
diff --git a/runtime/doc/change.txt b/runtime/doc/change.txt
--- a/runtime/doc/change.txt
+++ b/runtime/doc/change.txt
@@ -977,6 +977,11 @@
current line). This always works |linewise|, thus
this command can be used to put a yanked block as new
lines.
+ If no register is specified, it depends on the 'cb'
+ option, if 'cb' contains "unnamed", paste from
+ * register |quotestar|, if 'cb' also contains
+ "unnamedplus", use + register |quoteplus|, else paste
+ from the unnamed register |quote_quote|.
The cursor is left on the first non-blank in the last
new line.
The register can also be '=' followed by an optional


regards,
Christian
--
Sind wohl solche Zahlzeichen m�glich, die wenn ich das Blatt
herumkehre und alsdann ausspreche z.E. so viel bedeuten als die H�lfte
der vorhergehenden..
-- Georg Christoph Lichtenberg

Gary Johnson

unread,
Apr 23, 2012, 5:49:30 PM4/23/12
to vim...@googlegroups.com, vim-dev Mailingliste

Hi Christian,

Because the presence of "unnamedplus" trumps "unnamed", I think it
would be more clear if the options were described in this order.

if 'cb' contains "unnamedplus"
paste from "+
else if 'cb' contain "unnamed"
paste from "*
else
paste from ""

In English, something like this.

If no register is specified, it depends on the 'cb'

option. If 'cb' contains "unnamedplus", paste from
the + register |quoteplus|. Otherwise, if 'cb'
contains "unnamed", paste from the * register
|quote_star|. Otherwise, paste from the unnamed
register |quote_quote|.

Regards,
Gary

Gary Johnson

unread,
Apr 23, 2012, 6:07:24 PM4/23/12
to vim...@googlegroups.com
On 2012-04-23, Chris Jones wrote:
> On Mon, Apr 23, 2012 at 02:57:58AM EDT, Gary Johnson wrote:
> > On 2012-04-22, Chris Jones wrote:
> > > On Sun, Apr 22, 2012 at 02:11:47PM EDT, Gary Johnson wrote:
>
> [..]
>
> > What are you, my English teacher? It's probably a defensive habit to
> > give myself wiggle room. (Note the careful use of "probably" instead
> > of "think".) You want to be my shrink, too? No offense taken. And
> > none intended, honestly.
>
> :-)
>
> Forgive me... was in a ‘funny’ mood last night.. pixilated, perhaps..

No problem. As long as we're taking each other in good spirit. Or
spirits.

> From a more general perspective, the OP could use the ‘autocutsel’
> program that keeps X11's clipboard & primary selection in sync'.

Inspired by this discussion, I decided to install autocutsel and see
how that works out.

At work I have a Windows XP machine at my desk on which I run True
X-Mouse to get something approximating X behavior on Windows. For
development, I use NoMachine to display an X desktop (KDE) running
on a Linux machine in the back room. With all those windowing
systems and clipboard workarounds (True X-Mouse and autocutsel), it
will be interesting to see if it all actually works together.

Regards,
Gary

Bee

unread,
Apr 24, 2012, 12:41:40 AM4/24/12
to vim_use
When I read this thread I used the published line:

set clipboard=unnamedplus,unnamed,autoselect,exclude:cons\|linux

I use Mac, Win, Linux and each has different a version of [g]vim.
This "set" failed differently on each.

I read more and found if vim is compiled with "xterm_clipboard"
then the default for clipboard is: "autoselect,exclude:cons\|linux"
Otherwise it is: ""

What could be causing a failure?
The only two other options, unnamed or unnamedplus.

It turned out to be unnamedplus, and as I recently learned...
rather than test for has() or exists() I could simply use "silent!".

Now, in the Linux I am using, anytime I select and yank, or just yank,
both "* and *+ are filled. Going to any other app and paste with C-v
or middle mouse button click works.
--AND--
To my surprise, if I copy with C-c from another app like a web
browser, then go back to [g]vim, p or P or :put or :put! work!

I have the following in .vimrc
it is used on Mac, Win, and Linux
works for all gvim, MacVim and terminal vim,
depending on the compiled options.

The vmap and nmap remain for vim versions that are not gui or do not
have xterm_clipboard compiled.

if has("clipboard")
vmap ,y "+y
nmap ,p "+gP
" exclude:{pattern} must be last ^= prepend += append
if has("gui_running") || has("xterm_clipboard")
silent! set clipboard^=unnamedplus
set clipboard^=unnamed
endif
endif

Bill

Chris Jones

unread,
Apr 24, 2012, 2:33:50 AM4/24/12
to vim...@googlegroups.com
On Mon, Apr 23, 2012 at 06:07:24PM EDT, Gary Johnson wrote:
> On 2012-04-23, Chris Jones wrote:

[..]

> > From a more general perspective, the OP could use the ‘autocutsel’
> > program that keeps X11's clipboard & primary selection in sync'.
>
> Inspired by this discussion, I decided to install autocutsel and see
> how that works out.

Did the same but haven't looked into setting it up. Provided the tool
works as I think it does, namely that from the user's perspective it
makes the X clipboard and the PRIMARY selection behave as one, the
question is, do I really want that..? IOW, what are the implications?

It looks as if all the OP wants is X11 to work like Microsoft Windows
with just one single mechanism & clipboard..?

I rather dread revisiting these issues and fiddling with my environment
at this point.. I have about ten pages of notes on the subject that
would likely need to be updated.. I think I'll wait till I upgrade to
7.3 so I'm able to see for myself whether the new clipboard=unnamedplus
option makes any difference.

X was designed by computer scientists.. maybe that's the problem.

CJ

--
Hi! My name is bobby...

Sergey Vakulenko

unread,
Apr 27, 2012, 5:25:55 PM4/27/12
to vim_use
The problem is solved !
In this thread you can take a patch from Christian Brabandt which make
possible behavior described in Subject and test case.

http://groups.google.com/group/vim_dev/browse_thread/thread/1b50de7f4163dbb5/b36e113b3abebad9?lnk=gst&q=vakulenko&pli=1
Reply all
Reply to author
Forward
0 new messages