Mojave: Pasting text with control characters
I use git from within Terminal.app, and will occasionally end up (particularly after a merge resolution) with something like:
$ git status
...
Untracked files:
foo/bar/baz.orig
Where the whitespace before foo
is a tab character.
Since Mojave, copying and pasting that line (which I want to do to rm
the file) results in an alert (complete with loud bonk) asking me to confirm whether I want to paste the nasty control characters.
Anyone know if either:
- there's a way to disable the alert and just paste with the control characters, or
- there's a keyboard shortcut to paste without the control characters?
FWIW, I understand that there are other ways to remove the file (e.g. git status|grep orig$|xargs rm
) and I understand why, in the general case, you'd want to limit pasting of control characters.
Update your OS to 10.14.1 or later
It seems that Terminal in 10.14 would present this dialog when pasting a tab character, but the updated Terminal in 10.14.1 and 10.14.2 does not.
I was scratching my head as to why I was only able to reproduce this issue on one install but not another. Unable to find any settings that had any impact, I eventually tried updating the machine that showed the dialog, and it was gone.
So, keep in mind, the tab character, Unicode U+0009 "CHARACTER TABULATION" (UTF-8/ASCII hex 09) is simply a regular character. One that is very annoying to deal with, yes. But it is not a 'control character'.
This is different what happens when you press the key on your keyboard labeled tab, which might be mapped to an escape sequence/control character like '\t', which is different.
The specific issue you encountered is indeed "fixed" in 10.4.1+. However, this still doesn't mean you're not going to encounter this or similar errors.
For example, take the sequence:
printf '\033[34mEscape!\033[00m\n'
And try copy/pasting it (or its output) into different shells. Also try setting the Terminal.app setting 'Escape Non-ASCII input with Control-V', or using control-command-V, "Paste Escaped Text". Kinda weird stuff, eh?
- Within a terminal, your best bet for most things is:
- Generally, never use the ⌘C and ⌘V shortcuts to access the clipboard.
- Instead, pipe to the
pbcopy
command, and then usepbpaste
as needed.
So, this would be git status | pbcopy
, and for some this you might want to copy, you might need to combine stderr and stdout: git error 2>&1 | pbcopy
will result in an empty clipboard without 2>&1
.
This still may strip out proper control characters (anything that isn't valid UTF-8). But as you mentioned, you're already aware of why you might not want to copy and paste these. If you're set on capturing "control characters" as well as text, what we're really talking about is just capturing a byte stream, so it is probably best to avoid the clipboard altogether, which has never had the functionality of reproducing arbitrary byte sequences.
P.S. Have you considered switching to iTerm2?