home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 41.3 Find Out Terminal Settings with stty Chapter 41
Terminal and Serial Line Settings
Next: 41.5 Why Some Systems Backspace over Prompts
 

41.4 How UNIX Handles TAB Characters

TAB characters are used in a lot of places: tables, indented paragraphs, source code for programs, the output of many programs, and so on. UNIX handles TABs in a flexible way that's different from some other computer systems.

Most UNIX programs, printers, and terminals are set up to assume tabstops every 8 columns. That is, if the terminal or printer gets a TAB character on its input, it moves to the next tabstop position: column 9, 17, 25, etc. The UNIX system ( kernel, device driver ( 42.1 ) ) usually doesn't interpret TAB characters or set tabstops; it treats the TABs like any other character, passing them on to utilities or hardware like terminals.

You might want to use tabstop intervals other than 8. When I write programs, for example, an 8-character indent wastes space, so I use a 4-character indent. If you want to use different tabstops, too, you need to understand how TABs are handled.

41.4.1 TAB Is Just Another Character to UNIX

Typing TAB sends a single TAB character to the UNIX system. If you're editing a file, the editor probably puts that single TAB character into the file. Later, when you use cat ( 25.2 ) , pr ( 43.7 ) , lp ( 43.2 ) , and so on, they read each TAB and send out that single character to your terminal, printer, or whatever. The TAB is usually interpreted by the hardware device itself. Before that, it's a single character like any other. (But see the stty -tabs command below.)

If your terminal has a setup mode, enter setup mode and look at the tabstop settings. They're probably set at columns 9, 17, 25, and so on. When your terminal receives a TAB from the UNIX system, the terminal moves the cursor to the terminal's next tabstop.

For example, your terminal might have different tabstops - maybe 11, 21, 31, and so on. Let's say that you're cat ting a file. The programmer who made the file had her terminal tabstops set at the default 8-column intervals. When she typed it, she used TABs (shown as > in this example) to indent the lines. Her terminal showed the file this way:

% 

cat prog


while read line; do
>       set $line
>       for word in line; do
>       >       case "$1" in
...

If your terminal has tabstops set at 12 characters, the same file would look like this:

% 

cat prog


while read line; do
>           set $line
>           for word in line; do
>           >           case "$1" in
...

Neither the UNIX kernel nor the cat program did anything different to the prog file. The terminal interprets the TABs.

If you want to display or edit a file that has different tabstops than your terminal, what can you do?

  • expand
    Use a UNIX utility that "expands" (converts) TABs into spaces. On BSD systems, expand does the job. The terminal never sees the TABs in this case, so its tabstop settings are never used:

    % 
    
    expand prog
    
    
    while read line; do
            set $line
            for word in line; do
                    case "$1" in
    ...

    On System V, use pr with its -t -e options:

    % 
    
    pr -t -e prog
    
    
    while read line; do
            set $line
            for word in line; do
                    case "$1" in
    ...

    So, no matter what your terminal tabstops are, the file will look normal.

    If you want to use other tabstops, use an option. For instance, get 4-character tabstops with expand -4 or pr -t -e4 .

  • Tell your text editor to use different tabstops. The editor will probably "expand" the TABs into spaces before it sends them to your terminal. For instance, in vi , type:

    :set tabstop=4

    to edit programs with 4-character tabstops. The prog file from above would look like:

    % 
    
    vi prog
    
    
    while read line; do
    >   set $line
    >   for word in line; do
    >   >   case "$1" in
    ...

    although it has TAB characters in exactly the same places. If you have a text editor handy, try changing its tabstops while you display a file.

If you make a file with non-standard tabstops, it's a good idea to put a comment in the file so people will know. Or, before you save the file for the last time, expand the TABs into spaces. With vi on a system that has expand , for example, this command would convert TABs to spaces at 4-column tabstops:



:%!
 

:%!expand -4

The opposite of expand is, naturally enough, unexpand ( 24.6 ) . It converts spaces back to TABs at 8-column tabstops.

41.4.2 Telling UNIX to Expand TABs

I said above that the UNIX kernel and device drivers don't usually expand TABs into spaces. Sometimes, if you're having a lot of trouble with funny-looking tabstop settings, you might ask the device driver to expand the TABs into spaces. The command is:

% 

stty -tabs

Now in most cases, UNIX will not send TABs to your terminal. It's better to fix your terminal's tabstops, if you can.

- JP


Previous: 41.3 Find Out Terminal Settings with stty UNIX Power Tools Next: 41.5 Why Some Systems Backspace over Prompts
41.3 Find Out Terminal Settings with stty Book Index 41.5 Why Some Systems Backspace over Prompts

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System