Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I paste filenames/etc into Terminal all the time, and occasionally the contents of the clipboard isn't what I expect.

Often the result is several hundred lines of text (eg, source code) pasted into the command prompt, triggering god only knows what nastiness.

Is there some way to prevent bash from blindly executing whatever I paste in? I wish it would just paste the text, and let me decide after if I wanna execute it.

share|improve this question
add comment

5 Answers 5

up vote 6 down vote accepted

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).
share|improve this answer
    
I assume you mean Ctrl instead of Cmd. Also, without doing set -o emacs it seems to use $EDITOR. Thanks! this is perfect. –  Abhi Beckert Nov 22 '12 at 3:52
add comment

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.

share|improve this answer
    
the problem is, I paste things into terminal several hundred times every day. using a text editor in the middle would be a pain in the ass –  Abhi Beckert Nov 22 '12 at 3:43
add comment

I get this too!

My solution is to paste using Launchbar's clipboard manager, with the hotkey Cmd+\. This brings up a list of recent clippings, allowing me to preview the result before pasting: if I don't see the content I expect, I hit escape before releasing Cmd. I can also use an arrow key to 'enter' a text clipping, allowing me to see the content line by line.

I believe most launchers do this - Alfred (powerpack addon) & Quicksilver (free), for example.

share|improve this answer
add comment

You might want to look into a clipboard manager. I use a free, light weight one called Jumpcut. It allows me to keep multiple clipboards and see what's stored in them before actually pasting the contents, all from the keyboard.

There are many clipboard managers out there, so if Jumpcut doesn't float your boat, maybe another will better fit your needs

share|improve this answer
add comment

You could put this little Bash script somewhere in your PATH:

#!/bin/bash

read -p "Are you sure you wish to execute \`$(pbpaste)\`? (Y/n) "    
[[ $REPLY =~ ^[Yy]?$  ]] && pbpaste | bash

If I put these contents in /usr/local/pbexecute for example and make it executable, I could safely execute pbexecute from the command-line, which would prompt my confirmation before executing the contents of the clipboard.

An alternative approach is to launch a text editor with the contents of the clipboard from within the shell and automatically execute the edited contents upon exiting. For example:

#!/bin/bash

PBTEMP=$(mktemp -t pbtemp)
pbpaste > $PBTEMP
nano -t $PBTEMP
source $PBTEMP
rm $PBTEMP
share|improve this answer
    
Why the escaped back ticks? Using '' should work as well. –  patrix Nov 21 '12 at 6:53
    
@patrix If I don't escape the backticks, bash will already execute it while prompting. –  Gerry Nov 21 '12 at 6:54
    
Shouldn't omitting the backticks do the trick as well then? –  patrix Nov 21 '12 at 7:35
    
@patrix Oh, yes ofcourse, that's just a matter of preference on how you want to format the prompt. I chose backticks because they are the command substitution operator and are commonly used to visually markup commands (see StackExchange editing). –  Gerry Nov 21 '12 at 7:53
    
I'm ok with this :-) It just seems kind of complicated –  patrix Nov 21 '12 at 8:31
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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