Apple - Is there a way to make pasting into Terminal safe?

Assuming you are using bash: If you activate emacs shortcuts with set -o emacs you can also use Emacs to edit the command line:

  • Type Ctrl-XCtrl-E at the prompt to start Emacs
  • Paste and edit your clipboard content into the Emacs buffer
  • Use Ctrl-XCtrl-C to exit Emacs and automatically run the command(s).

Use bracketed paste.

Bracketed paste is a feature where the terminal tells the shell that you pasted something instead of just typing it, so the shell can not run the command until you press Enter.

Terminal.app supports this, but not all terminals do.

Likewise, not all shells support it either. The version of bash that comes with OS X does not support bracketed paste (as of the time of writing, it's v3.2.57).

However, zsh does support bracketed paste:

zsh-bracketed-paste

Zsh is mostly compatible with bash (even scripts will probably work, unless you use some sort of really obscure feature), and better in many ways—for example, it even has multi-line command editing!

zsh-multiline-command


The simplest solution is probably to use a text editor as a buffer where you can double-check the contents, then copy from the text editor to Terminal. If you use TextEdit for this, you'll probably want to make sure it's in plain text mode (Format menu → Make Plain Text; ⇧⌘T) to avoid issues like links pasting as links, not URLs.

The other thing to note is that Bash will attempt to execute everything preceding a newline character, so if you're copying a block of text, you may want to ensure that your selection stops after the last character, rather than including the end of the line.

You want this: selection without newline

Not this: selection with newline

Of course, if you're copying stuff with newlines within the text (i.e. multiple paragraphs), then you'll need to manually edit the selection.

Tags:

Terminal