When I paste into my terminal session the shell immediately executes the command without me pressing the enter key.
I really don't know how to disable that behaviour.
I'm using the preinstalled terminal on MacOS Yosemite.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
bash 4.4 or newer and in terminals that support bracketed paste a la
xterm, you can do:
bind 'set enable-bracketed-paste'
set enable-bracketed-paste to your
That will cause the copy-paste buffer to be inserted at the prompt instead of the characters in it to be interpreted as if typed (you could still have problems if that buffer contains characters like
^Z and your terminal emulator doesn't filter them out).
zsh does that by default since version 5.1.
For other shells or terminals, see also: How can I protect myself from this kind of clipboard abuse?
This happens when you paste a "newline" character. For example, if you're pasting multiple lines or you unknowningly copy a carriage return at the end of the line. AFAIK there's no way to actually disable this behaviour but there are ways to mitigate it.
The easiest way is to just copy one line at a time and miss off the last character, manually adding it afterwards.
Or, as the_velour_fog points out below, you could also paste it into an editor that shows non-printed characters and reselect without the newline.
There's one or two workarounds floating around for GNU/Linux distros but no idea if the concept could be ported over to MacOS. Regardless, take a look here and see if there's a similar file in mac that you could alter. Unfortunately, I doubt it.
Before pasting, type a single-quote, then paste. control-c and up-arrow to edit it as one giant line.
double-quotes also work, in case the text contains single but not double quotes. It's ok if it contains
$ or other things that expand inside double quotes, because you're not actually running the quoted giant line as one command.
You can't actually edit the whole thing right there, because newlines do start a continuation prompt (not the official terminology) instead of making one big long line.
given these 4 lines:
echo ls foo bar
pasting as I suggest will result in this on your screen:
$ 'echo > ls > foo > bar <or cursor here if you didn't include a trailing newline > [cursor here]
You can then press control-C and up-arrow. (control-C destroys everything on the same line as the cursor, so press return first if there's useful text there.)
An alternative is to close the quote and press return, then up-arrow that.
Anyway, after up-arrow
$ 'echo ls foo bar
control-a and remove the leading
'. Use control-left and control-right to move quickly through the paste block (by words). Or if your terminal setup doesn't support control-arrow-keys, alt-b and alt-f.
The literal newlines do actually separate commands, and these 4 lines will go into your command history separately. You might want to change them to
&&. (I don't know a trick for that, just use key-repeat for forward or backward-word to get there quickly and do it manually.)
This happens because your paste ends with a newline, and terminal emulators basically feed the paste to bash character by character. In this process, bash doesn't know human typist from a machine really.
I usually just pay extra attention when doing copy-pasting that it doesn't include a newline, but there's one trick that you can try: before paste, in bash, type in
C-x C-e. It should launch
$EDITOR with an empty file. Whatever you save in that file and exit, bash will execute. So you can now paste your command to the editor, possibly manipulate things further and then run them all in one go.
I am NOT related to the company, I am a very satisfied customer. Try this terminal program: http://www.emtec.com/zoc/index.html. There is also a Mac version available.
If I violate with this answer any SE rule please let me know and I delete the answer.
When I understood the other answers correctly this problem may be caused by the terminal program. The ZOC terminal program is a professional software which is perfectly designed for the work. It pastes the text you selected without any additional character. You can log your sessions in a separate text file. And lot more
The terminal program has an option
Paste without final 'newline'. May be this helps.