📜 ⬆️ ⬇️

Safe "ctrl + v" in the terminal

I think everyone at least once neatly 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" accidentally turned out to be selected, respectively, you already ran the command .


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

Total - you need to intercept the data from the clipboard by pressing "ctrl + v", check for the presence of "\ n" and then "call" ctrl + v. It is not logical to do a buffer check for all application programs, and for example in gnome-terminal “Paste” is assigned to “Shift + Ctrl + V” by default, then I replaced it with “Ctrl + V”. Next you need to put a buffer check on the usual combination “Shift + Ctrl + V”.
There are many options to do this that depend on the working environment used. Specifically, in my case it is
')
~ / .config / openbox / rc.xml
<keyboard> <keybind key="SCv"> <action name="Execute"> <command>/home/user/scripts/safe_paste.py</command> </action> </keybind> </keyboard> 


Do not forget chmod + x /home/user/scripts/safe_paste.py.

/home/user/scripts/safe_paste.py
 #!/usr/bin/env python from paste_it import main main() 


/home/user/scripts/paste_it.py
 #!/usr/bin/env python import os from subprocess import Popen, PIPE def main(): #    ,   xsel sys_exec = Popen('xsel', stdout=PIPE) stdout = sys_exec.stdout.read() #       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"', shell=True) return False paste = Popen('%s/pasteit &' % os.path.dirname(__file__), shell=True) return stdout if __name__ == '__main__': main() 


Splitting the code into 2 files will give a slight performance boost, of course, and you can immediately assign paste_it.py.

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

Next you need to call "crtl + v" to insert a safe text. Well, if the content is not safe, then open the text editor \ throw away some characters \ remove "\ n" automatically, then it is already more convenient for someone, the implementation will also not be difficult. For me personally, it is enough to know that I have touched something extra when selecting.

There were many ways to call “ctrl + v” from under Linux:


Need to write yourself.
On the c / s ++, I scored a walk, since it would not kick the floor.

A very compact java solution was implemented:

PasteIt.java
 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(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.delay(20); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); } catch (AWTException e) { e.printStackTrace(); } } } 


javac PasteIt.java

On a not entirely new laptop, the call of this class took 1-2 seconds, it was not very comfortable, but it worked.

Gcj looks abandoned but

gcj -O2 --main = PasteIt PasteIt.java

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

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

The solution turned out to be not the best, pulling a bunch of technologies, but the code turned out to be very compact and clear, which will easily allow everyone to adjust everything for themselves. The performance turned out quite normal.

Replacing xsel with stackoverflow.com solutions, using mingw , you can also use GCJ, but I’m not quite sure of the need for all of this to use in powershell \ cmd, but it may be useful for putty.

Source: https://habr.com/ru/post/165317/


All Articles