1

I tried reading the manual for unexpand as well as read the answer here but I do not understand how unexpand works with a tablist as an argument.

When I try the following (2 spaces and 3 spaces expecting them to converted to tabstops)

$ unexpand -t 2,3,4
  12**345***678

I get

12\t\t345***678

* represents a space (I used * for clarity). I'm not sure why there are two tabstops after 2 and no tabstops after 5 even though there are 3 blanks.

I also tried the following (expecting blanks in the 2nd and 5th columns to be converted to tabstops)

$ unexpand -t 2,5
  1*23*4567

I get

1*23*4567

I also tried

$ unexpand -t 2,5
  1**2*****345

I get

1\t*2*****345

I can't understand how the program interprets the tablist argument when there is more than one arg.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
kovac
  • 111
  • 2

1 Answers1

2

unexpand does the reverse of expand. If you do unexpand -t 2,3,4 | expand -t 2,3,4, you should get back to the original.

The numbers are not the distance between the tabs, but their position (there distance from the left of the screen).

With -t 2,3,4 you're setting the first tab stop at offset 2 (third column), second at offset 3, third at offset 4, and that's it.

  ↓↓↓
0123456
$ printf '%b|\n' '\t' '\t\t' '\t\t\t' | expand -t2,3,4
  |
   |
    |

The 1, 2, 3 tabstops are located at:

  123

If you match that against your:

  123
12**345***678

The 3 is at the third tabstop, so you need two tabs after 2 to get there, the first tab gets you to the second tabstop (as you've already reached the first), and the second to the third tab stop. There is not tab stop after than so your spaces are not converted to tabs.

If you wanted the second tab stop to be 3 columns after the second, and the third to be 4 columns after the second, you'd need -t 2,5,9:

  ↓  ↓   ↓
0123456789
$ printf '\t1\t2\t3\n' | expand -t 2,5,9
  1  2   3

Then you'd get:

          ↓  ↓   ↓
        01234567890
$ echo '12  345   678' | unexpand -t 2,5,9 | sed -n l
12  345\t 678$

Beware of the differences between (un)expand and tabs (the command that sets the tabstops of your terminal for those that support it):

  • for tabs, columns are numbered from 1 instead of 0
  • you use tabs -8 (instead of expand -t 8) to set tabstops every 8 columns (in effect, like tabs 9 17 25 33..., though you can also use tabs 9 +8 +8 +8...).
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • So, at the offsets, if there is a blank, it will be replaced with a \t? Like `unexpand -t2,3,6 "12**34*5"` will set columns 3, 4 and 5 to \t if these columns previously contained blanks? Here since I have blanks at offsets 2,3 and 6 I expected output "12\t\t34\t5" but I get "12\t*34*5". – kovac Feb 10 '22 at 11:02