Safer command line paste in iTerm 2

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.

Brett Terpstra

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.

This content is supported by readers like you.

Join the conversation