Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I am in vim I can change the tab size with the following command:

:set ts=4

Is it possible to set tab size for cat command output too?

share|improve this question
up vote 16 down vote accepted

The first command here emulates the formatting you see in vim. It intelligently expands tabs to the equivalent number of spaces, based on a tab-STOP (ts) setting of every 4 columns.

printf "ab\tcd\tde\n" |expand -t4   


ab  cd  de

To keep the tabs as tabs and have the tab STOP positions set to every 4th column, then you must change the way the environment works with a tab-char (just as vim does with the :set ts=4 command)

For example, in the terminal, you can set the tab STOP to 4 with this command;

tabs 4; printf "ab\tcd\tde\n" 


ab  cd  de
share|improve this answer
good to know (1). expand (from coreutils package) and tabs (from ncurses package) command, (2). and the meaning of ts is [T]ab [S]top – LiuYan 刘研 Mar 12 '13 at 8:37

There's no notion of tabs or tab stops in cat; the program just funnels the inputs to the output and treats tabs like any other character. If the output device happens to be a terminal, tabs will be handled according to whatever behavior the terminal is configured to provide.

Systems implementing POSIX.1 have a command called tabs(1) that will adjust the terminal's concept of how tabs should be displayed. Depending on a particular tab layout is not considered a good idea, as someone may send your file to some other device such as a printer that won't do what you intended.

When you adjust ts in vim (or plain vi), all you're doing is adjusting how the editor interprets tab characters when displayed. It has no bearing on what ends up in the file.

share|improve this answer
Thanks for explanation. How is the printer configured to treat tabs? – Meysam Feb 27 '12 at 19:36
Most printers treat them the same way as most terminals do by default: the left margin and every eight characters afterward. – Blrfl Feb 27 '12 at 19:59

Just use the following code:

tabs -n

Where n is the number of spaces you want tabs to correspond too. In order to not having to do this every time you start the shell, just edit your .bash_profile in ~/ and add the above line to the end of the file.

For further info about the tabs command, refer to:

man tabs
share|improve this answer
Just beware that permanently changing your default tabs width might screw up other commands (for example ls out put may look not properly aligned). – Gustavo Bezerra Feb 2 '14 at 14:58

Based on the above answers and examples, it would seem that the actual command the OP wanted is...

cat somefile | expand -t4

This works for me on Red Hat 6.4.

share|improve this answer

To expand on the already given answers, expand can also take a list of tab stop positions. This is useful if the content lengths of the varlious columns varies a lot.

I came over this requirement today when I wanted to make the output of openssl ciphers more readable:

$ openssl ciphers -v 'HIGH'|tr -s ' ' '\t'|expand -t31,41,57,70,90
ECDHE-RSA-AES256-GCM-SHA384    TLSv1.2   Kx=ECDH         Au=RSA       Enc=AESGCM(256)     Mac=AEAD
ECDHE-ECDSA-AES256-GCM-SHA384  TLSv1.2   Kx=ECDH         Au=ECDSA     Enc=AESGCM(256)     Mac=AEAD
ECDHE-RSA-AES256-SHA384        TLSv1.2   Kx=ECDH         Au=RSA       Enc=AES(256)        Mac=SHA384
ECDHE-ECDSA-AES256-SHA384      TLSv1.2   Kx=ECDH         Au=ECDSA     Enc=AES(256)        Mac=SHA384
ECDH-ECDSA-AES128-SHA          SSLv3     Kx=ECDH/ECDSA   Au=ECDH      Enc=AES(128)        Mac=SHA1
AES128-GCM-SHA256              TLSv1.2   Kx=RSA          Au=RSA       Enc=AESGCM(128)     Mac=AEAD
AES128-SHA256                  TLSv1.2   Kx=RSA          Au=RSA       Enc=AES(128)        Mac=SHA256
AES128-SHA                     SSLv3     Kx=RSA          Au=RSA       Enc=AES(128)        Mac=SHA1

CAMELLIA128-SHA                SSLv3     Kx=RSA          Au=RSA       Enc=Camellia(128)   Mac=SHA1
PSK-AES128-CBC-SHA             SSLv3     Kx=PSK          Au=PSK       Enc=AES(128)        Mac=SHA1

Using only expand -t31 would blow up the width of the output from around 100 characters to more than 160 characters.

share|improve this answer
An easier way to do that is to use column: openssl ciphers -v 'HIGH' | column -t – muru Feb 10 at 15:57

According to the manpage, cat can not do it on its own. But you can e.g. run the output from cat through tr filter to replace the tabs with any number of spaces you wish:

cat somefile | tr '\t' '  '

will replace the tab character with two spaces.

Update: as pointed out in the comments to this post, this actually does not work. Nevertheless, I'm keeping the answer as an example of how not to do it.

share|improve this answer
It's actually replacing '\t' with only one space, no matter how many spaces are in between the second pair of quotes ' ' – Meysam Feb 27 '12 at 14:52
tr doesn't work that way.. each byte of arg 1 is substituted by each corresponding byte of arg 2..... Therefore printf '\t' |tr '\t' 'ळ' prints out a single byte whose hex value is \xE0 .. which is the first byte of the three UTF-8 encoded bytes which make up the character (whose Unicode CodePoint value is U+0933) – Peter.O Feb 27 '12 at 14:59
Ah, of course. Thanks for catching my silly mistake! – Petr Uzel Feb 27 '12 at 15:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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