Windows/Putty users get easily into a habit of automatic copying of selected text into clipboard (select to clipboard) and pasting it via right-click. You can see similar features in other terminals, so I was expecting it will eventually land in a Linux GUI terminals as well.
Please note – if you are running Gnome terminal in Centos7, you can jump straight into Dependencies sections below.
I was slightly unpleasantly surprised when I found that for example developers of gnome terminal won’t add this feature – not even as an option – https://bugzilla.gnome.org/show_bug.cgi?id=739215 So I saw it as a challenge with two choices – either to fork it (with all the maintenance overhead) or to create on-the-fly “patch” by using scripting language with a bonus that it would be easily adaptable to other terminals as well. I decided for a second choice. As on Windows there exist quite powerful scripting tools, I expected there must be something even better on Linux too… The short answer is: There isn’t. It is most likely because Linux is open-source, so things can be tweaked in multiple ways… In final I decided to go for python-xlib. Also please note that X has multiple clipboards, which bear the names CLIPBOARD, PRIMARY, SECONDARY. In final I created two separate scripts (daemons), where each does it’s separate job:
terminal_copy_on_select.py – copies selected text immediately from PRIMARY to CLIPBOARD in terminal window – if PRIMARY clipboard is changed
terminal_right_click_paste.py – swaps middle and right click buttons if terminal window is activated and restores them back when other window is activated (so you can actually still access former right-click properties via middle button)
I also tried to create systemd startup files and I sort of got them working, but then I realised that I need to start my scripts after user logs into window manager (WM) and not before and in my view at the current stage the systemd is not ready yet to support this task, so I created xdg autostart files instead. Also I wanted to create daemons (scripts) which will NOT periodically look for changes – and eat CPU and battery – but which will be event driven – so script will only make an activity in case of event from WM. (I also found that popular parcellite is NOT event driven and even if you do nothing, it eats CPU and battery). Let’s start:
First of all – scripts I created are tested on Centos7 Gnome3 install with Gnome terminal, but they should work on other OSes and terminals as well. The key point is that your terminal application contains word “term” in it’s WM class name (more on that in Tuning section below). Scripts are written and tested in Python 2.7, and should work fine in Python 3 as well. Please let me know if you used them successfully in other conditions – such as Ubuntu or other terminals.
Before you install any package, verify it is not already installed.
On .rpm based distributions (Fedora,CentOS,Redhat,Suse) you do it via:
$ rpm –q python
$ rpm –q python-xlib
On .deb based distributions (Ubuntu,Mint,Debian) you do it via:
$ dpkg -l python
$ dpkg -l python python-xlib
You will have python most likely installed, but python-xlib is not part of default Centos7 install so you need to download it for example from here:
Then install it (as root):
# rpm -ivh python-xlib-0.15-0.8.rc1.el7.noarch.rpm
You can find python-xlib packages for other distributions here: http://pkgs.org/download/python-xlib
Download file terminal_copy_paste.tar.gz file from my GitHub page – https://github.com/jj999/linux_terminal_copy_on_select_right_click_paste/raw/master/terminal_copy_paste.tar.gz
Then install it (as root):
# tar -C / -pxvzf terminal_copy_paste.tar.gz
Above command will unpack below files into these folders:
If you do not need one of the features (for example because you use custom clipboard manager or your terminal program supports one of the features), feel free to delete file you do not need (especially .desktop file).
Now you can log-off and log-back in into your Window manager and both programs should be running (you do not need to restart the computer, just log-off/log-in).
After you log back in, you can open terminal and test copy/past functions. If it works as desired, you are done now and you can skip the rest.
For more detailed test, check if daemons are running:
$ ps -ef | grep python | grep terminal_
You should see two lines with process information displayed. If nothing is displayed, there is a problem. In that case, try to start terminal_right_click_paste.py daemon manually as in below example (as normal user) and check for error:
Error will be most likely related to missing dependency – check that section. If you are just testing you can interrupt this daemon with Ctrl+C. Also please note you cannot terminate terminal_copy_on_select.py with Ctrl+C, because it is graphical daemon (without visible GUI elements) so to interrupt it, you need to kill it via another terminal window – to do that check first two steps of Removal/Uninstall section below.
If you wish to adapt both daemons to different terminal (which does not have word “term” in it’s WM class name, then run script which will display the WM class name of your terminal:
It will also display WM class names of other windows as well as you switch between them. So you can see when the program sees the window change and when it does not.
Once you have the new WM class name of your terminal, replace word “term” to the calss name you found in variable terminal_classs_name at the beginning of below two script:
1. find process numbers of running daemons (as normal user or root) – they will be in the second column in output of below commands:
$ ps -ef | grep python | grep terminal_
2. kill them (as normal user or root):
$ kill found_process_number1
$ kill found_process_number2
3. remove installed files (as root):
# rm /usr/local/bin/terminal_copy_on_select.py
# rm /usr/local/bin/terminal_right_click_paste.py
# rm /etc/xdg/autostart/terminal_copy_on_select.desktop
# rm /etc/xdg/autostart/terminal_right_click_paste.desktop
4. you may restore your mouse buttons after terminal_right_click was terminated (as normal user or root) – this step is optional:
$ xmodmap -e “pointer = 1 2 3”
I tested programs for different scenarios and so far I found one:
When you right-click on terminal window frame showing it’s name, menu for resizing/moving will be displayed, and when you right-click again from this menu to the terminal, right-click will be set to middle-button. However if you click second time with left-click, right-click will be OK… Let me know if you find anything else.
Enjoy, leave comments and support… 😉