How to change tab width for ls(1)?

Hi all!

I recently decided to change terminal tab stop width from default 8 to 4, because when I do some coding in the terminal, strings go too much to the right (almost out of the screen) after a couple of tabs. So I managed to change my terminal emulator capabilities and re-create the termcap(5) database. Now, when I hit tab it visually looks like 4 spaces, so far so good.

But when I type ls (or ls -C, which forses multi-column output, the default for terminal) the output gets wrong alignment. I figured that that's due to the fact that by default ls(1) assumes that tab stop size is 8, meanwhile I set it to 4. In the Internet I found the information about -T (or --tabsize) option for `ls` command. You can read about it in the Slackware 3.1 manual page, for example. However, I didn't find the equivalent option for [cmd=1]ls[/cmd] in FreeBSD. Neither it has a TABSIZE environment variable or something like that. I looked into the source code of `ls` and it seems that it indeed does not allow to specify different size for tab stops. Here's a small snippet from /usr/src/bin/ls/print.c:
C:
int tabwidth;
if (f_notabs)
    tabwidth = 1;
else
    tabwidth = 8;

Well, I understand that for now I can just change the value of this variable to 4 and recompile `ls` from sources, but it doesn't seem to be a flexible solution. After all, is that the only possible way to make it work?

Has someone ever managed to have a custom tab stop width and decent output alignment for programs like ls(1)?

Thank you.


Artem.
 
[…] So I managed to change my terminal emulator capabilities and re-create the termcap(5) database. […]
Err, what? All you need to do is invoke tabs(1):​
Bash:
tabs -4
[…] However, I didn't find the equivalent option for ls(1) in FreeBSD. […]
I don’t think ls(1) even uses actual tabs (the \t byte I mean). I have in my .profile:​
Bash:
# `ls` minimum column widths:
#
# 1. file serial number:  2⁶⁴ − 1  requires 20 digits
# 2. block count:         7
# 3. number of hardlinks: 4 as some directories contain 1000+ files
# 4. user name:           8 POSIX™ requires support for
# 5. group name:          8 at least 8 characters
# 6. flags:              16
# 7. file size:           4 because our `ls(1)` writes `K`, `M`, `G` suffixes
# 8. file name:          14 POSIX™ minimum maximum file name length is 14 B
LS_COLWIDTHS='20:7:4:8:8:16:4:14'
[…] But when I type ls (or ls -C, which forces multi-column output, the default for terminal) the output gets wrong alignment. […]
The column‑width in “short listing” mode are computed dynamically. A directory could contain files that prohibit the reasonable display of multiple columns.​
 
Err, what? All you need to do is invoke tabs(1)
Heh, I did that too. I just have some issues with my previous terminal emulator capabilities and invoking tabs -4 complained something like: terminal doesn't support tabs, so I did termcap(5) stuff just to be sure. Nevermind, just my local problem.

I don’t think ls(1) even uses tabs. I have in my .profile:
Aren't the settings you've sent about long listing format (i.e. ls -l)? It actually works great, I'm asking about let's say "short" format, which is ls -C or just ls, when output is the terminal.
 
use ls --color and will use spaces instead
Not kinda my case - I disabled color support for `ls` in src.conf(5) ( WITHOUT_LS_COLORS).
By the way, I didn't know it forces ls(1) to use spaces. Where did you find this information? I can not see it in --color option description in the manual page.

or maybe install gnuls and alias it to ls
Well, I think that I don't want to have another binary for `ls` just for this case - seems like an overkill.

But thank you for suggestions, covacat! For now, I think, I will stick with the "change the source and recompile" strategy; maybe I'll find something better and report it here.
 
Back
Top