deetee
deetee

Reputation: 85

How to set the tab size of terminal (without "tabs" or "expand")

I would like to list some lines of text (stored in a variable or file) starting at i.e. column 3 but doing this without overwriting the columns 1 and 2.

So starting each line with a tab (\t) seems to be an appropriate solution. Unfortunately the tab size of my terminal seems to be fixed to 8 (instead of desired 3).

Unfortunately I do not have the command tabs because I am working with busybox/ash. Even expand does not help because columns 1 and 2 will be overwritten with spaces.

So is there an alternative to tabs? Maybe awk (printf) could be a solution if there is a possibility to set the tab size?

Example:

TEXT="line1
line2
line3"
echo "$TEXT" | sed 's/^/\t/'

provides

         line1
         line2
         line3

but I want

  line1
  line2
  line3

Thanks in advance.

Upvotes: 3

Views: 3792

Answers (2)

riteshtch
riteshtch

Reputation: 8769

In order to avoid replacing the 1st 2 columns, You'll need to manually move the cursor to the 3rd column and start writing from there. You can't use spaces(using expand) since that would overwrite the 1st 2 columns with spaces.(tabs don't overwrite though)

You can use echo along with the following escape sequence to position the cursor at line/row L and column C

  \033[<L>;<C>H
     Or
  \e[<L>;<C>H
     Or
  \e[<L>;<C>f
     Or
  \033[<L>;<C>f

Here is an example:

Output of the 1st echo command:

shell@OnePlus2:/ $ clear
shell@OnePlus2:/ $ echo "12\n34\n56"                                           
12
34
56
shell@OnePlus2:/ $ 

Now we'll write another echo command that does the following:

  1. move cursor to line 2, column 3 and write line1 after 12
  2. move cursor to line 3, column 3 and write line2 after 34
  3. move cursor to line 4, column 3 and write line3 after 56
  4. write out a newline \n so that you skip the current prompt(that has the echo command) and go to next line(this makes cursor come at the proper position).

after running the above echo command we'll get the following:

shell@OnePlus2:/ $ echo "12\n34\n56"                                           
12line1
34line2
56line3
shell@OnePlus2:/ $ echo -e "\e[2;3fline1\033[3;3fline2\e[4;3fline3\n"          
shell@OnePlus2:/ $ 

Note: 12, 34 and 56 are not overwritten and you have also written out line1, line2 and line3.

Note: the 1st clear command is very important so that you have just the prompt on your terminal, because the line and column values are hardcoded(2,3,4 lines and column 3 for each line). You can however modify my example according to your needs.

If you want to just print from the 3rd column and spaces before that you can use:

shell@OnePlus2:/ $ printf "  %s\n" 'line1' 'line2' 'line3'                     
  line1
  line2
  line3
shell@OnePlus2:/ $ 

For more info on escape sequences see: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

if you have access to tput see: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html

PS: even I'm using a restricted shell, not a full blown bash shell:

shell@OnePlus2:/ $ echo $SHELL
/system/bin/sh
shell@OnePlus2:/ $ 

As you can see I've connected to my android shell's sh via adb. Since you are using busybox which is also available for android it should be no problem. However we aren't using any high end shell features, just using echo and escape sequences.

PPS: If you can give us more insight on how are your input files organized.. We can just use some other multi file commands such as paste, awk etc

Edit1: my bad. corrected my answer

Edit2: since you don't have access to tabs or expand command, you can also use setterm -regtabs 2 to set tab width/size to 2(it doesn't work in xterm's though giving the error: setterm: terminal xterm does not support --regtabs).
However I am able to run it in text mode virtual terminals accessed via Ctrl+Alt+F[1-6].

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54525

Rather than use control sequences which require both row and column to be specified, your terminal probably supports one which moves within the current row. Referring to XTerm Control Sequences:

CSI Pm `  Character Position Absolute  [column] (default = [row,1])
          (HPA).

which would be

printf '\033[%d`' $col

in the shell. Also, your text may overwrite previously-written data after the text. You can clear that with another control sequence:

CSI ? Ps K
          Erase in Line (DECSEL).
            Ps = 0  -> Selective Erase to Right (default).
            Ps = 1  -> Selective Erase to Left.
            Ps = 2  -> Selective Erase All.

sed is a little awkward for producing escape characters. Here is a suggestion in awk:

TEXT="line1
line2
line3"
echo "$TEXT" | awk '{ printf "\033[3`%s\033[K\n", $0; }'

Upvotes: 2

Related Questions