Setting tab width in C++ output in bash

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

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'

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).