Safer command line paste in iTerm 2

[Tweet : nvALT]

If you’ve ever copied a shell command from somewhere and accidentally included a trailing newline, you know that hitting paste in your terminal can run the command before you have a chance to edit it. There are a few ways around this, but here’s a quick tip for iTerm 2 users.

In Preferences -> Keys -> Global Shortcut Keys, add a new shortcut assigned to Control-Command-V (or Command-Option-V, or whatever makes sense and doesn’t override existing shortcuts…). Set the action to “Run Coprocess” and enter pbpaste | tr -d "\n" as the command. When you trigger this, it will take whatever is in the clipboard and remove all the newlines from it, ensuring that it won’t execute when pasted.

Now, when you want to paste something directly to the command prompt, use this new shortcut instead of ⌘V for extra peace of mind.