Sometimes if text that is on the clipboard is pasted into shell and it contains a return character it will execute even though that wasn't the intention. Sometimes even multiple lines may be pasted in by mistake and then unintended commands could be run.
I understand in a perfect world, these mistakes would never happen, but is there a way to prevent this to avoid the risk of human error?
What you want is called 'bracketed paste', a feature that's available in some shells.
If your version of bash supports it, you can turn it on for the current session like so:
Now try it out by pasting multiple lines:
The shell recognises that the text was pasted (not typed), and waits with a prompt for your confirmation. If it all looks safe to proceed, hit the Enter key. If not, hit Control-C to cancel.
If you'd like to enable bracketed paste for every new session, add the command to your .inputrc file:
Another option is to switch from bash to zsh, where bracketed paste is enabled by default. To replace your current shell with a zsh shell:
With zsh, pasted text gets highlighted, which is nice. Again, hit Enter to execute the command/s or Control-C to cancel.
If you like zsh, and wish to make it the default shell:
If you're using PuTTY on Windows, here's an AutoHotKey script which will detect if you're trying to paste something into PuTTY that has one or more newlines and if so will confirm you're wanting to do so.
Tip: you can hit the space bar or keypad enter key as an easy way to press "Yes".
Note: this script uses ctrl+v to paste into PuTTY, but you could replace
^v
withRButton
if you wanted to hook this into the right-click-to-paste default functionality of PuTTY.Use
Ctrl
+X
Ctrl
+E
(“edit current line”) before pasting. This will start your default editor; you have to save and quit, then the pasted commands are executed.I had the same problem, but then I made a habit of always copying a single word into clipboard, after I'm done pasting. If you force this habit 3-4 times then your hands will do it automatically from there on. I have never mis-pasted since I began this routine.