Safe "ctrl + v" in the terminal

I think every time, at least, carefully selecting a piece of code \ console command from the window of your favorite browser, pressing "ctrl + c", then "ctrl + v" in the active window of the terminal emulator was very surprised - "\ n" turned out to be highlighted, so you already ran the command .

I propose an easy and simple solution to this problem. Since the best programming language is python, this is the only thing I somehow know, we will use it.

Total - you need to intercept data from the clipboard when you press "ctrl + v", check for the presence of "\ n" and then again call "ctrl + v". It is not entirely logical to do a buffer check for all applications, and for example, in the gnome-terminal “Paste” is assigned to “Shift + Ctrl + V” by default, I replaced it with “Ctrl + V”. Next, you need to put a buffer check on the familiar combination “Shift + Ctrl + V”.
There are many options to do this, which depend on the working environment used. Specifically in my case, this is

~ / .config / openbox / rc.xml

One must not forget chmod + x /home/user/scripts/

#!/usr/bin/env python
from paste_it import main

#!/usr/bin/env python
import os
from subprocess import Popen, PIPE
def main():
    # берем текущие значение буффера, через вызов xsel
    sys_exec = Popen('xsel', stdout=PIPE)
    stdout =
    #если количество строк в буфера не равно 1, то сообщим об это этом
    if len(stdout.splitlines()) != 1:
        n_detected = Popen('notify-send "\\n detected"', shell=True)
        return False
    #самое интересно, об этом чуть ниже
    codes = map(ord, stdout)
    for code in codes:
        if code <= 31 or code == 127:
            bad_code_detected = Popen('notify-send "bad code detected"', 
            return False
    paste = Popen('%s/pasteit &' %
        os.path.dirname(__file__), shell=True)
    return stdout
if __name__ == '__main__':

Splitting the code into 2 files will give a small performance boost, of course, you can immediately assign

In addition to "\ n" , , namely 0-31 and 127, which can also cause unexpected terminal behavior, can also get into our buffer , therefore, you need to inform about this.

Next, call “crtl + v” to insert the safe text. Well, if the content is not safe, then open a text editor \ throw out some characters \ automatically remove "\ n", then it’s more convenient for someone, the implementation will also not be difficult. Personally, it’s enough for me to know that I hurt something extra when highlighting.

There were many ways to call "ctrl + v" from under Linux:
  • xdotool - it’s a wonderful thing - in Centos 6.3 there are no turnips in the turnips;
  • xvkbd - is built by imake (Imake is a deprecated source code configuration and build system), but did not
  • python-uinput - from a sparrow cannon, device emulation, hal rule
  • python-virtkey - also failed to build
  • xautomation - was in turnips, but very ancient and worked terribly, and ctrl was sticky
  • under windows there are autohotkey, autoit, pywin32

You have to write yourself.
On c \ c ++, I scored on a walk, since it won’t work out with a half kick.

A very compact java solution was implemented:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class PasteIt{
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
        } catch (AWTException e) {


On a brand new laptop, calling this class took 1-2 seconds, it was very uncomfortable, but it worked.

GCJ looks abandoned, but

gcj -O2 --main = PasteIt

and the a.out binary was created, which I renamed pasteit and it works for 0.1 sec.

Now, as usual, I press “ctrl + shift + v”, and if something is not suitable in the buffer, I will get a notification, I can also use “ctrl + v” if I need to insert data without checking.

The solution was not the best, pulling a bunch of technologies, but the code turned out to be very compact and understandable, which will easily allow everyone to fine-tune everything for themselves. Performance turned out to be quite normal.

Replacing xsel with solutions from, using mingw you can also use GCJ, but I'm not quite sure if all this is needed for use in powershell \ cmd, but it might be useful for putty.

Also popular now: