31

Right, this is a slightly annoying feature because something this happens, and other times it doesn't, very odd.

I paste a command into the terminal with a view to tweaking its arguments, and sometimes it executes the second I paste it. Sometimes it doesn't.

This ranges from embarrassing ( posting nonsensical comments on an IRC ) to dangerous.

How can I shut this feature off for good? I never, never, never want the terminal to auto execute when I paste to it.

27

There's no auto execution. You probably are also copying a line break (or more) after the command.

If you paste a command with a break, the shell (command line) thinks that you hit the break (Enter, Return) button by yourself.


This answer only explains why OP is getting the undesired behaviour. However, in another answer, there is a real solution to the OP’s problem.

| improve this answer | |
16

One real solution is to open editing mode with ctrl-x ctrl-e and then safely paste your clipboard into your editor of choice (commonly emacs or vim). After you are done editing the command, save and exit and it will run in the command line. This is a good habit for long or complex commands, especially when pasting commit hashes into git commands, and can help you avoid some scary results in the long run.

The editor can be changed by setting the VISUAL and EDITOR variables (these have different meanings, see here).

| improve this answer | |
  • wow, there is always something to learn after decades of using linux :) thanks, this is the only answer that actually solves the problem – törzsmókus Jun 29 '18 at 7:35
  • Sounds like it might work for a real linux terminal, but unfortunately this isn't helpful for PuTTY users. Any other alternatives? – Codesmith Oct 27 '18 at 18:00
  • 1
    @Codesmith I believe that Pawel Chioch's comment on the accepted answer may be helpful to you: serverfault.com/questions/731022/… – borrascador Oct 28 '18 at 0:56
  • 1
    Good point @borrascador! Not sure how I missed that. Thanks! :) – Codesmith Oct 29 '18 at 0:23
6

Prior to pasting type a # then paste your line. It will not execute and you can remove the # and hit enter when the modifications have been completed.

| improve this answer | |
  • You could also use true AKA :, which is basically a no-op command. – wjandrea Dec 22 '17 at 21:56
  • 1
    @WJAndrea I would say this is less advisable since it will clobber the exit status ($?)... true isn't really a nop, it returns exit status 0. A comment, on the other hand, is entirely ignored and therefore won't alter $?. – brhfl Jan 25 '18 at 19:52
  • 7
    this will only stop the first line from executing. if OP pastes a text with at least two line breaks, they are screwed again. – törzsmókus Jun 29 '18 at 7:29
1

As others have mentioned, the problem occurs when the pasted text contains newline characters. With that knowledge, your question is more-or-less the same as this one on Server Fault. Different site, so I'll repeat my answer here…

What you want is called 'bracketed paste', a feature that's available in some shells.

Assuming you're using a recent version of the bash shell, you can turn it on for the current session like so:

bind 'set enable-bracketed-paste on'

Now try it out by pasting multiple lines:

echo Hello world
echo Again, I say, hello!

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:

cd ~
echo "set enable-bracketed-paste" >> .inputrc

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:

exec zsh

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:

chsh -s /bin/zsh
| improve this answer | |
0

I use this alias to paste and have my command NOT-executed

alias nonewlinepaste='history -s `xsel -b`; sleep 1; xdotool key Up 1>/dev/null'  # xsel ref. https://unix.stackexchange.com/a/96398/17671  # xdotool ref. https://unix.stackexchange.com/questions/267704/command-to-simulate-keyboard-input#comment1038650_267705
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.