MacOS – Why is Terminal.app corrupting the tcsh history

command linemacosterminalunix

I've had an ongoing problem that appears to be specific to OS X and Terminal.app: periodically, I get garbage in my tcsh history that looks like this:

^[[1m^[[4mcomputername:~/Code/emacs-source ( 1827 ) $^[[0m^[[24m logout

Somehow my prompt (including markup control characters) is getting stored as part of the command history. In my .history file, the corrupt commands are repeated with the same command number:

#+1391563259
[1m[4mcomputername:~/Code/emacs-source ( 1827 ) $[0m[24m
#+1391563259
Use "logout" to logout.
#+1391563259
[1m[4mcomputername:~/Code/emacs-source ( 1827 ) $[0m[24m logout 

This seems to be a result of closing Terminal.app windows and tabs, but I'm not sure why. To fix it — since I have history merging enabled — I've had to resort to closing all tcsh sessions and removing the corrupt commands from .history. Is there some way to prevent this from happening in the first place?

Best Answer

Are you cutting/pasting commands and picking up some of your prompt by accident?

It's not quite the answer you're looking for but throw sed -i.bak -e "s/\x1b[[0-9;]{1,5}m//g" .history into your .logout or cron it and you shouldn't have to clean it manually again.