A setting for Mac OSX Terminal to pause after paste before executing
January 14, 2020 7:05 PM   Subscribe

Before I updated from 10.12 to 10.14 I made a lot of use of a setting in terminal that I think was enabled with a "defaults write" command, that made it so that pasting (Cmd-V) in Terminal did not immediately execute the contents you pasted in the shell.

I could abort with Ctrl-C if you pasted the wrong thing. If I pasted something enormous it didn't wreak havoc. If I pasted a small multi-line command, I could cursor around to edit a portion before executing. Now I can't find it anywhere in google. Does anyone know how I did that and whether it's still possible in 10.14?

I also used it all the time to paste and look at tabular text, and with option-drag, Terminal was often my go-to lightweight tool for copying a rectangular portion of the selection.
posted by spbmp to Computers & Internet (7 answers total)
 
Not sure if it helps on Mac OSX... On Linux X11 cut-n-paste stuff it all depends on whether you include the newline in your cut part. If you select a whole line or block you get the newline when you paste and it executes. If you're more careful of your selection... when you paste it just stays there until you press the newline yourself.
Another trick might be to just put a '#' at the beginning of the line before you paste so that the line is a comment. Or (if Bash) to do a 'fc' command (fix-command) to open up $EDITOR and paste into that, then you can edit and execute on exit.

But I can imagine a 'strip final newline from paste' option, but also figure that sounds like something that Apple would take away just because.
posted by zengargoyle at 7:51 PM on January 14, 2020 [1 favorite]


The feature you're describing is called bracketed paste.

This StackExchange says that you would normally enable it by adding set enable-bracketed-paste to your ~/.inputrc, but unfortunately, Mac OS's built-in version of Bash is too old and doesn't support it.

I think your options are updating Bash (to a newer version via Homebrew, perhaps) and then enabling bracketed paste, or switching to Zsh or another shell that supports it by default. I'm not sure what you might have done with a "defaults write" command — maybe you switched your default shell?

If you don't mind switching to Zsh, since Zsh is the default shell in Catalina (and presumably future versions of OS X), that might not be a bad solution.
posted by Kadin2048 at 7:58 PM on January 14, 2020 [3 favorites]


First swap the Terminal app for iTerm2. Preferences > Profiles > Terminal lets you enable paste bracketing.

oh-my-zsh is an easy way to explore the zsh shell, with lots of community support.
posted by bendy at 8:21 PM on January 14, 2020


Best answer: You can use TextEdit to do the rectangular selection.
posted by mekily at 8:47 PM on January 14, 2020 [1 favorite]


Best answer: Ok, strangely (and in part I think due to attempts a while back) I've got it working the way I liike, but I still would like to know how to turn it on and off.

It works in zsh (my usual shell) or bash, but not tcsh.

When I run defaults read com.apple.Terminal, I see inside a deeper nested structure an entry called ConvertNewlinesOnPaste set to 1 that I suspect is doing the work, *but* it's in the hierarchy as "Window Settings" -> "Basic" -> "ConvertNewlinesOnPaste". I can't remember how I set that flag (or find any reference about ConvertNewlinesOnPaste online).

Ah! I think I set it in the plan-old Preferences under Profiles -> Basic -> Advanced -> "Paste newlines as carriage returns"

Playing with that setting does change the results of
defaults read com.apple.Terminal | grep -C 3 ConvertNewlinesOnPaste

... though it's a little unpredictable when the setting will "work". A new window seems to help after I broke it, but the old window works sometimes.

... and I'm still curious how to set with defaults write, but after tryinng a bit it doesn't appear that the defaults command has the capability to dig into a hierarchy like that.
posted by spbmp at 6:49 AM on January 15, 2020


Response by poster: The preference in the GUI is working fine. It just helps to make a new window after you set the flag.
(If you use a different "Profile" change the setting for that one as well.)
posted by spbmp at 7:06 AM on January 15, 2020 [1 favorite]


The fancy way to do what you want is by hitting ctrl - x then ctrl - e while in a terminal. This will open the current command in a text editor, or the previous one if the line is blank. Set the VISUAL or EDITOR variables first to tell Bash which text editor you want to use. If the command looks good, save and quit to run the command. If you want to discard it, quit the editor without saving.

Search for "edit-and-execute-command" on this page for brief documentation.

echo $VISUAL #check if variable is set
VISUAL=nano #set editor to nano

Set the variable in your ~/.bashrc file if you want to make it permanent.
posted by mammal at 5:50 PM on January 16, 2020 [1 favorite]


« Older How do you recover from burnout?   |   Flooring contractor in Queens, NY Newer »
This thread is closed to new comments.