52

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.

3

8 Answers 8

37

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 behavior. However, in another answer, there is a real solution to the OP’s problem.

3
21

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).

4
  • wow, there is always something to learn after decades of using linux :) thanks, this is the only answer that actually solves the problem Jun 29, 2018 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, 2018 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/… Oct 28, 2018 at 0:56
  • 1
    Good point @borrascador! Not sure how I missed that. Thanks! :)
    – Codesmith
    Oct 29, 2018 at 0:23
19

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
1
  • 1
    Bracketed paste was exactly what I was searching for. This should be the top answer as it is more detailed and encompassing. Jun 12, 2022 at 8:39
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.

3
  • You could also use true AKA :, which is basically a no-op command.
    – wjandrea
    Dec 22, 2017 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, 2018 at 19:52
  • 8
    this will only stop the first line from executing. if OP pastes a text with at least two line breaks, they are screwed again. Jun 29, 2018 at 7:29
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
0

I get around this when copying commands that are too long by typing with another editor, like Atom. For some reason, copy-pasting long commands from vim and nano can be sketchy. Copying from Atom never gives me this headache.

0

put an anti slash then paste your code before it then use ctrl+c to cancel the cmd. you can afterward use your arrows to get back to the cmd

  • step 1 : put an antislash \

  • step 2 :

    your cmd \

  • step 3 :

    Ctrl+c

0

Use this "Here Document" trick:

  1. Type <<
  2. Paste your code or Ctrl+Shift+V
  3. Hit Ctrl-C
  4. Use Up Arrow Key to access the last command
  5. Hit Ctrl-A to move to the start of command or use Left Arrow Key
  6. Delete << from start of command
  7. Hit Enter

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .