5

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.

  • Could you please specify your OS and terminal emulator application? – egmont Nov 20 '16 at 10:49
  • xclip may be useful as in printf|xclip – hildred Nov 21 '16 at 1:04
  • In the first version of this question I by mistake wrote echo -t where it of course should have been echo -e. Some answers & comments below thus took me too literally. Sorry. – Axel Bregnsbo Nov 26 '16 at 9:59
7

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.

  • So are you saying that it should work for me as long as the tabs are not at the end ? – Axel Bregnsbo Nov 20 '16 at 11:05
  • In terminals based on a recent enough VTE: yes. GNOME Terminal 2.16 might be a bit too old for this, though. – egmont Nov 20 '16 at 11:09
  • GNOME 2.16 was released in Sep 2006. The "smart tab" feature of VTE was implemented in Nov 2007. May I kindly recommend you to upgrade your systems to something reasonably recent? :) – egmont Nov 20 '16 at 11:15
  • I have just issued a ticket to IT. If this fixes my issue you deserve a big upvote. However, in that case your answer is confusing. It should start with the sentence "Use gnome-terminal 3.xx". – Axel Bregnsbo Nov 20 '16 at 11:27
  • I'm not going to track down the very exact version where this appeared (some 2.22-ish perhaps), let alone it's not even gnome-terminal itself rather than vte that matters. I silently assume in all my answers I give on this site that the system being used is a sanely recent one, I go into version-dependent details if the change occurred no more than 2-3 years ago. For such an extreme as this one the comments clarify it. – egmont Nov 20 '16 at 13:08
3

If you use

printf "xx\t\tyy"

that should expand to a real tab-character. The behavior of echo in regard to bash has been erratic. I tested bash on my Debian 7, and found that neither echo -t nor /bin/echo -t gave a tab, while printf did (redirecting the output to a file to be sure). Here is the script:

#!/bin/bash
echo -t "xx\t\tyy"
echo done
/bin/echo -t "xx\t\tyy"
echo done
echo "xx\t\tyy"
echo done
/bin/echo "xx\t\tyy"
echo done
printf "xx\t\tyy"
echo done

and output to the terminal:

-t xx\t\tyy
done
-t xx\t\tyy
done
xx\t\tyy
done
xx\t\tyy
done
xx              yydone

That's distinct from the additional problem of putting characters on the terminal screen. As a rule, most terminals only have spaces selectable for cut/paste (irregardless of whether you print a tab or not). For that —

  • Yes you right: echo -t "xx\t\tyy" | hexdump shows wierd stuff whereas printf "xx\t\tyy" | hexdump shows the 0x09 tab character. However still get spaces on the terminal. – Axel Bregnsbo Nov 20 '16 at 11:00
  • My comment above is junk. When I use the proper echo -e the results from echo and printf piped to hexdump are identical. The problem is my old gnome-terminal version 2.16.0 as another answers points out. – Axel Bregnsbo Nov 26 '16 at 10:04
2

A bit of a tanget, but if all you want is to get the output into your clipboard so you can paste that into Excel, you can try using something like xclip. Note that you might have to install it. With xclip I can write something like this.

echo -e "xx\t\tyy" | xclip -selection c

It is now in my clipboard I can paste it into Excel. If echo doesn't work you can try printf instead.

If you don't want to type all of that every time you can create an alias instead.

alias xclip="xclip -selection c"
echo -e "xx\t\tyy" | xclip

And if nothing else you can always redirect the output you want to a file and just open it in a text-editor and copy what you need as tabs should stay as tabs in the file.

echo -e "xx\t\tyy" > tmpfile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.