Thanks for your detailed and informative help T3slider :-) It has gone a long way toward evolving a satisfactory solution, although I'm still:
- using ~/.Xresources rather than ~/.Xdefaults (it works and I don't know any difference between them that signifies .Xdefaults is more appropriate -- always willing to learn, though).
- Putting the Perl functions file, clipboard, in the default directory, /usr/lib/urxvt/perl
As installed, urxvt follows X tradition: selecting text copies it to the X PRIMARY buffer from where it can be pasted using both "middle mouse click" and Shift+Insert.
That's fine until you want to use urxvt with applications that use the X CLIPBOARD buffer, which also use the
Common User Access (CUA) Ctrl+C and Ctrl+V key combinations. Then it would be useful to be able to:
- Copy from urxvt to the X CLIPBOARD buffer and paste from there using Ctrl+V, menus etc.
- Copy from other programs using Ctrl+C, menus etc into the X CLIPBOARD buffer and then paste it into urxvt.
Which key combinations to use?
- Ctrl+C is not practicable in terminals where it is used to generate Break.
- Ctrl+V is more practicable but is used in bash, as in vi, to allow the insertion of a control character into the command string.
- Ctrl+Shift+C/V are widely used in other terminal emulators but Ctrl+Shift+<anything> seems not to be available in urxvt where Ctrl+Shift is the key combination for unicode data entry.
- Super+C/V is easy to reach and rarely used -- so that's the one I chose.
The only other design decision was not to bother with a key combination to copy the selection to the X CLIPBOARD buffer but to extend the original "selecting copies to the X PRIMARY" to "selecting copies to both X PRIMARY and X CLIPBOARD".
This scheme was implemented by installing xclip (xsel is an alternative) ...
... by creating /usr/lib/urxvt/perl/clipboard (root:root, rw-r--r--) ...
Code:
#! perl
sub on_sel_grab {
my $query = $_[0]->selection;
open (my $pipe,'| /usr/bin/xclip -in -selection clipboard') or die;
print $pipe $query;
close $pipe;
}
sub paste {
my ($self) = @_;
my $content = `/usr/bin/xclip -loop 1 -out -selection clipboard` ;
$self->tt_write ($content);
}
sub on_user_command {
my ($self, $cmd) = @_;
if ($cmd eq "clipboard:paste") {
$self->paste;
}
}
... and by adding corresponding lines to ~/.Xresources (~./Xdefaults could also be used but I did not test it)
Code:
URxvt.keysym.Mod4-v: perl:clipboard:paste
URxvt.perl-ext-common: default,matcher,clipboard
Notes:
- Acknowledgements and thanks to the many who shared information: all in this LQ thread, all contributors to the ArchLinux WIKI urxvt page, Sergei Steshenko who suggested a variation on the ArchLinux WIKI Perl that made it work on my system in this LQ thread, the Play Linux Blog page on urxvt copy-and-paste and, of course, the authors of urxvt itself (listed in the urxvt man page).
- The on_sel_grab() and on_user_command() functions in /usr/lib/urxvt/perl/clipboard are urxvt "hook"s as described in the urxvtperl man page.
- xmodmap can be used to show whether Super (actually Super_R and Super_L) are mapped to the "mod4" used in URxvt.keysym.Mod4-v
- The Super key is sometimes known as the Windows key.
- The "clipboard:paste" in URxvt.keysym.Mod4-v: perl:clipboard:paste identifies the user command which is handed to the on_user_command() function.
- The "clipboard" in URxvt.perl-ext-common: default,matcher,clipboard tells urxvt to load the clipboard Perl file when it initialises the terminal.