Module 57 fmt

Previous Table of Contents Next


Module 57
fmt

DESCRIPTION

The external fmt command performs simple right margin justification text formatting. A list of files are read in and the input is massaged to be as close as possible to 72 columns wide. Spacing at the beginning of input lines, blank lines, and spacing between words are left unmodified. If no files are provided as arguments, fmt reads from the standard input.

The fmt command was originally written to format messages before mailing, but it may be useful for other simple formatting tasks . It is useful for formatting sections of text printed by a shell script.

COMMAND FORMAT

Following is the general format of the fmt command.

 fmt [ -cs ] [ -w width ] [ file_list ] 

BSD (Berkeley)
fmt [ filename_list ]

Options

The following list describes the options and their arguments that may be used to control how fmt functions.

-c Use crown margin mode. The indentation of the first two lines is preserved, with the rest of the paragraph aligned with the second line.
-s Do not join short lines to form longer ones. Only split lines. This prevents short lines, such as examples of program code, from being combined.
-w width Format output text up to width columns. The default width is 72 columns.

Arguments

The following argument may be passed to the fmt command.

file_list A list of files to format.
The fmt command is commonly used without files being specified, that is it is often a filter.

EXAMPLE

A popular use of fmt is from within the vi editor. Assume you have created the following file and you are on line one. The cursor is under the T of This.

 This is a brief message to the Control Unit group.      The new hp is now on-line. To send prints to it, type lj filename.      If you have questions contact Sys. Adm. (x480).      The printer is located in the stock room. 

Now type !} fmt and press Return. This command sends the paragraph to the fmt command as standard input. Then vi reads the standard output of the fmt command and replaces the paragraph with the formatted output. Your paragraph now resembles the following screen.

 This is a brief message to the Control Unit group. The new hp is now on-line. To send prints to it, type lj      filename. If you have questions contact Sys. Adm. (x480). The printer is located in the stock room. 

Obviously fmt is very limited in what it can do, but it is very fast for simple text formatting.

RELATED COMMANDS

Refer to the fold command described in Module 58.

RELATED FILES

The fmt command reads input from a list of files or from the standard input. It writes to the standard output.

APPLICATIONS

You can use the fmt command to format messages before sending them to another user via mail . Another use is for formatting memos or comments in source code.

TYPICAL OPERATION

In this activity you use the fmt command to format the entire buffer of text while you are in vi . Begin at the shell prompt.

1.    Type vi fmted and press Return . This will put you in vi .
2.    Type i and enter two or three paragraphs of text, such as the following.
 cj> vi fmted      i      The fmt command will format this buffer screen.      By using the vi filter interface it is sent out to the standard input of a UNIX command and the      standard output of the command is read back into the vi buffer.      The contents of the buffer are overlayed with the new output               of the command.      The fmt command will reformat the jagged right end of the lines to be as close as possible to 72 columns.      It basically only performs right margin justification. 
3.    Now type 1G to move to the top of the buffer. The cursor is at the top left corner of the screen (buffer).
4.    Type !Gfmt and press Return to send the contents of the buffer to the fmt command. The buffer will be redrawn with the newly formatted output. The output should resemble the following.
 The fmt command will format this buffer screen. By using the vi filter      interface it is sent out to the standard input of a UNIX command and the      standard output of the command is read back into the buffer. The contents      of the buffer are overlayed with the new output of the command.      The fmt command will reformat the jagged right end of the lines to be as close as possible to 72      columns. It basically only  performs right margin justification. 
5.    Type :q! and press Return to exit vi without saving the text.
6.     Turn to Module 58 to continue the learning sequence.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net