Add Copy-Paste Functionality to Xterm, Rxvt like Terminals

Xterm, Rxvt like terminals do not support the traditional Ctrl C Ctrl V kind of copy paste functionality. Does anybody need it? Well I guess not, because you can highlight text, and then use the middle-click to paste it wherever you want. However if anybody needs it, this is what you need to do. Go ahead and install the ttyecho tool from this post of mine.

  1. Download the source code from the post hyperlinked above and copy it to a file ttyecho.c
  2. Execute make ttyecho in the directory that you have created the ttyecho.c file in
  3. Copy the ttyecho binary into the bin directory under your home directory: mkdir -p ~/bin/; cp ttyecho ~/bin/
  4. Make it setuid root: sudo chown root:root ttyecho; sudo chmod +s ttyecho

Now install xclip and x11-utils. These commands are for debian based system, use the appropriate commands/gui for your linux distro.

sudo aptitude install xclip x11-utils

Now copy the below code into a file called xpaste in your home’s bin directory.

#!/bin/bash
PTS=$(ps --ppid $(xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}') | awk '/_NET_WM_PID\(CARDINAL\)/{print $NF}') -o tty,comm | grep bash | cut -d ' ' -f 1 | cut -d '/' -f 2)
TEXT=$(xclip -o -sel clipboard)
~/bin/ttyecho /dev/pts/$PTS "$TEXT"

Copy the next code snippet into a file called xcopy in your home’s bin directory.

#!/bin/bash
xclip -o sel pri | xclip -sel clip

Lets make both the scripts executable.

chmod +x ~/bin/xpaste ~/bin/xcopy

Now we’ll need to make keyboard shortcuts for these scripts. My recommendation is Control Shift B for xcopy and Control Shift X for xpaste. Ofcourse you can use anything you want, but don’t use Control C or Control V. That won’t work. To make the keyboard shortcuts, you can consult the listed tutorials.

Tutorial for KDE Users
Tutorial for Gnome Users

Now copy/paste inside xterm like you would using Ctrl C Ctrl V.

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

1 comment

Leave a Reply