Bash Terminal – Consistent Way to Mangle Terminal

bashshellterminal

I'm on OSX 10.11.1 and occasionally my bash terminal gets mangled. It often happens when I accidentally cat a binary file. The result can be seen on the image below. The output becomes weird, and I can't type ascii characters anymore.

enter image description here

Even though this happens occasionally, I couldn't find a way to consistently reproduce the issue. Online search recommends doing cat /bin/*, but that works sporadically, only after a couple dozen tries.

I want to do this so I can find an easy solution how to handle this in tmux.

How do I consistently get bash to a "mangled" state? Is there maybe a magic unicode character that can do this?

Best Answer

This looks like the DEC special graphics character set.

Reading the xterm control sequences docs, it sounds like the terminal uses those when receiving ESC ( 0.

So you should be able to reproduce using

printf '\033(0'

or

printf '\033(0' > corrupt-my-terminal
cat corrupt-my-terminal

And get back using

printf '\033(B'

which according to the same page selects USASCII.


Other ways to restore the state include

tput sgr0  # resets all terminal attributes to their defaults

and

reset      # reinitializes the terminal

You could tput sgr0 in your PROMPT_COMMAND (bash), or precmd (zsh) to ensure it always gets reset automatically.


Or you could just make sure to use less, vim, or anything other than cat to view a file.

To make less act like cat and automatically quit if the file is under one page long, run less -FX, or do export LESS=-FX.

Or if you don't want to always use those less options, make a new alias, e.g.

alias c='less -FX'
Related Question