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.

I have a c++ program and I use the tab character "\t" to organize my output to a human-readable columns. However, 8-spaces-wide column in bash is too narrow for me. How can I increase it?

Note: In C++ forums people say: "C++ just outputs the character and is not responsible for the visible output". In linux forums people say: "It is the program you use to output to terminal that is responsible for this, probably less or so."

share|improve this question
    
how are you viewing your program on the terminal, is it using a pager such as less or and editor like vi? –  1_CR Oct 24 '12 at 16:44
    
No, no "viewer". I open the terminal and type ./a.out or whatever is the name of the binary. –  yo' Oct 24 '12 at 16:45
    
If you don't use any viewer, it's the programs role to format the output accordingly. If you were to use a pager or a text editor or processor, maybe, but just MAYBE, this intermediary software could represent tabs as N (N being customizable) spaces. –  Spidey Oct 24 '12 at 17:17
    
If I redirect the ouput to a file like ./a.out > file.txt, then the file contains the tab character. So obviously, the program sends the tab character to the terminal and it's a role of the terminal to typeout it correctly. –  yo' Oct 24 '12 at 17:20
    
Also, see unix.stackexchange.com/questions/46368/… –  Alex Leach Oct 24 '12 at 17:37

3 Answers 3

up vote 3 down vote accepted

You can change the tab stops in your terminal using the terminal database, which you can access several ways from C++ (for example, ncurses). You can also access it from shell using tput.

You'd want to start by clearing the tabs (tput tbc). Then move the cursor to each column you want a tab stop in (tput hpa 10 for column 10, for example). Then finally set the tab stop (tput hts). Repeat the positioning and tab setting for each tab stop you want. Example:

echo -e '0\t1\t2\t3\t4\t5\t6\t7\t8'
tput tbc
for ((i=0; i<`tput cols`; i+=10)); do
    tput hpa $i
    tput hts
done
tput hpa 0
echo -e '0\t1\t2\t3\t4\t5\t6\t7\t8'
share|improve this answer
    
So I can put something like tput tbc tput hpa 10 tput hts tput hpa 20 tput hts ... into my .bashrc and just be happy? –  yo' Oct 24 '12 at 17:46
    
@tohecz yes, you can put that in your bashrc, but that'll change tabstops every bash shell you have. You may not want it for all your shells. You can also use ncurses, etc. in your program, and then just have it take effect when you run your program. –  derobert Oct 24 '12 at 17:48

So, the printed tab character is fixed within the source code of the program. I don't think that the display of the tab character in bash can be edited in a shell setting.

I'm just guessing here, but I think the representation of the tab character is embedded within the character encoding set that your terminal program is using. The ASCII character set defines the tab character, but the UTF-8 character set doesn't seem to. I don't think any character encoding set uses a different width for the tab character, so I think you're out of luck unless you want to write your own character set and use it, but that sounds like a headache waiting to happen.

Instead, have you tried the pr command?

PR(1) User Commands PR(1)

NAME pr - convert text files for printing

To swap tab characters for 10 spaces, you could do this:-

./a.out | pr --expand-tabs=10 -t
share|improve this answer
    
pr paginates the output, which is frowned upon. Can this be disabled? –  yo' Oct 24 '12 at 17:31
    
@tohecz the -T option disable pagination –  derobert Oct 24 '12 at 17:33
    
-t option. Will update answer... –  Alex Leach Oct 24 '12 at 17:33
    
Ok, so it seems to help, thanks. However, having to pipe it seems a bit strange to me and means you have to call it another way to output it tab-seperated to a file etc. –  yo' Oct 24 '12 at 17:38
    
Well, you could output it to file once, complete with tabs, and then run pr .. directly on the tab-delimited file. e.g. ./a.out > output.txt ; pr --expand-tabs=10 -t output.txt –  Alex Leach Oct 24 '12 at 17:43

C++ isn't responsible for the width. I had a longer response typed up but it really became unnecessary when I did a test...

Basically, use tabs (part of the ncurses5 package)... e.g.

zsh> tabs 4 # 4 space width tabs
zsh> ./a.out

This will format to your width you want automatically. No need to pipe (which doesn't help if you have interactive work).

share|improve this answer
    
I've edited in which package the tabs command comes from in case someone doesn't have it installed. Feel free to revert my edit if you do not approve. –  derobert Oct 24 '12 at 19:25
    
Thanks @derobert I wasn't sure as it came preinstall on my CentOS 6 box and I didn't check to see what provided it. –  sparticvs Oct 24 '12 at 22:02

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.