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 catting 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?

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 ToolsNext: 41.5 Why Some Systems Backspace over Prompts
41.3 Find Out Terminal Settings with stty Book Index41.5 Why Some Systems Backspace over Prompts

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