Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

5 Answers 5

up vote 15 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   

Output

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" 

Output

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
add comment

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
add comment

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
add comment

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 at 14:58
add comment

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
3  
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
3  
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
add comment

Your Answer

 
discard

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.