Bash Terminal – Output Tab Character

bashclipboardterminal

How do I output a tab character (ASCII hex 0x09) on the terminal window ?

In all my experiments the tab character is changed to spaces when it appears on the terminal. E.g.

$ echo -e "xx\t\tyy"
xx              yy

which is not want I want since the space between xx and yy is filled with 14 spaces and not 2 tab characters. I tried stty tab0, stty tab1, stty tab2 and stty tab3, but all gives the same result.

I am using GNOME Terminal 2.16.0 and Red Hat Enterprise Linux Client release 5.9.

Background:
I want to mouse-select the text on the terminal and paste into Excel. When I do this from Emacs (with 0x09 tabs between the fields) the fields end up in different columns. I like this, and want the same behavior when copy-pasting from the terminal. However, at present all the fields end up as one string in the first column. When I have spaces (0x20) between fields in Emacs, the behavior is the same as when copy-pasting from the terminal.

Best Answer

Tab is not a printable character. Tab is a control character that usually advances the cursor (but not at the end of line), leaving the characters that it's jumping through unchanged.

gnome-terminal (and other vte-based emulators) have a special hack that it tries to preserve tabs for copy-paste purposes, however, it still loses them at a soft linebreak. Other emulators might also have such a hack, but typically they don't.

See also the conversation at https://bugzilla.gnome.org/show_bug.cgi?id=769316.

Related Question