[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]
Web dsl.org

Viewing Text

Dealing with textual matter is the meat of Linux (and of most computing), so there are going to be many chapters about the various aspects of text. This first chapter in this part of the book shows how to view text on your display screen.

There are many ways to view or otherwise output text. When your intention is to edit the text of a file, open it in a text editor, as described in Text Editing.

Some kinds of files -- such as PostScript, DVI, and PDF files -- often contain text in them, but they are technically not text files. These are image format files, and I describe methods for viewing them in Previewing Print Files.

NOTE: To learn how to browse files and their contents in a Web browser, see Browsing Files.

Perusing Text

Use less to peruse text, viewing it one screen (or "page") at a time. The less tool works on either files or standard output -- it is popularly used as the last command on a pipeline so that you can page through the text output of some commands. For an example, see Redirecting Output to Another Command's Input.

zless is identical to less, but you use it to view compressed text files; it allows you to read a compressed text file's contents without having to uncompress it first (see Compressed Files). Most of the system documentation in the `/usr/doc' and `/usr/share/doc' directories, for example, consists of compressed text files.

You may, on occasion, be confronted with a reference to a command for paging text called more. It was the standard tool for paging text until it gave way to less in the early to mid-1990s; less comes with many more options -- its most notable advantage being the ability to scroll backward through a file -- but at the expense of being almost exactly three times the size of more. Hence there are two meanings to the saying, "less is more."

Perusing a Text File

To peruse or page through a text file, give the name of the file as an argument to less.

This command starts less and displays the file `README' on the screen.

You can more forward through the document a line at a time by typing [], and you can move forward through the document a screenful at a time by typing [PgDn]. To move backward by a line, type [], and type [PgUp] to move backward by a screenful.

You can also search through the text you are currently perusing -- this is described in Searching Text in Less.

To stop viewing and exit less, press [Q].

Perusing Multiple Text Files

You can specify more than one file to page through with less, and you can specify file patterns in order to open all of the files that match that pattern.

This command starts less, opens in it all of the files that match the given pattern `/usr/doc/FAQ/unix-faq-part*', and begins displaying the first one:


NOTE: When you specify more than one file to page, less displays each file in turn, beginning with the first file you specify or the first file that matches the given pattern. To move to the next file, press [N]; to move to the previous file, press [P].

Commands Available While Perusing Text

The following table gives a summary of the keyboard commands that you can use while paging through text in less. It lists the keystrokes and describes the commands.
[] Scroll back through the text ("up") one line.
[] Scroll forward through the text ("down") one line.
[] or [] Scroll horizontally (left or right) one tab stop; useful for perusing files that contain long lines.
[PgUp] or [SPC] Scroll forward through the text by one screenful.
[PgDn] Scroll backward through the text by one screenful.
C-l Redraw the screen.
/pattern Search forward through the file for lines containing pattern.
?pattern Search backward through the file for lines containing pattern.
< Move to beginning of the file.
> Move to end of the file.
h Display a help screen.
q Quit viewing the file and exit less.

Outputting Text

The simplest way to view text is to output it to standard output. This is useful for quickly looking at part of a text, or for passing part of a text to other tools in a command line.

Many people still use cat to view a text file, especially if it is a very small file. To output all of a file's contents on the screen, use cat and give the file name as an argument.

This isn't always the best way to peruse or read text -- a very large text will scroll off the top of the screen, for example -- but sometimes the simple outputting of text is quite appropriate, such as when you just want to output one line of a file, or when you want to output several files into one new file.

This section describes the tools used for such purposes. These tools are best used as filters, often at the end of a pipeline, outputting the standard input from other commands.

NOTE: Tools and methods for outputting text for printing, such as outputting text in a font, are described in Converting Plain Text for Output.

Showing Non-printing Characters

Use cat with the `-v' option to output non-printing characters, such as control characters, in such a way so that you can see them. With this option, cat outputs those characters in hat notation, where they are represented by a `^' and the character corresponding to the actual control character (for example, a bell character would be output as `^G').

In this example, the output of cat is piped to less for viewing on the screen; you could have piped it to another command, or redirected it to a file instead.

To visually display the end of each line, use the `-E' option; it specifies that a `$' should be output after the end of each line. This is useful for determining whether lines contain trailing space characters.

Also useful is the `-T' option, which outputs tab characters as `^I'.

The `-A' option combines all three of these options -- it is the same as specifying `-vET'.

Outputting a Beginning Part of a Text

Use head to output the beginning of a text. By default, it outputs the first ten lines of its input.

You can specify as a numeric option the number of lines to output. If you specify more lines than a file contains, head just outputs the entire text.

To output a given number of characters instead of lines, give the number of characters to output as an argument to the `-c' option.

Outputting an Ending Part of a Text

The tail tool works like head, but outputs the last part of its input. Like head, it outputs ten lines by default.

It is sometimes useful to view the end of a file on a continuing basis; this can be useful for a "growing" file, a file that is being written to by another process. To keep viewing the end of such a file, use tail with the `-f' ("follow") option. Type C-c to stop viewing the file.

NOTE: You can achieve the same result with less; to do this, type F while perusing the text (see Perusing Text).

Outputting a Middle Part of a Text

There are a few ways to output only a middle portion of a text.

To output a particular line of a file, use the sed tool (see Editing Streams of Text). Give as a quoted argument the line number to output followed by `!d'. Give the file name as the second argument.

To output a region of more than one line, give the starting and ending line numbers, separated by a comma.

You can also combine multiple head or tail commands on a pipeline to get the desired result (see Redirecting Output to Another Command's Input).

In the preceding example, three commands were used: the first five lines of the file `placement-list' are passed to tail, which outputs the last line in the output (the fifth line in the file); then, the last head command outputs the first character in that last line, which achieves the desired result.

Outputting the Text between Strings

Use sed to select lines of text between strings and output either just that section of text, or all of the lines of text except that section. The strings can be words or even regular expressions (see Regular Expressions -- Matching Text Patterns).

Use the `-n' option followed by `'/first/,/last/p'' to output just the text between the strings first and last, inclusive. This is useful for outputting, say, just one chapter or section of a text file when you know the text used to begin the sections with.

To output all of the lines of text except those between two patterns, omit the `-n' option.

NOTE: For a more thorough introduction to sed, see Editing Streams of Text.

Outputting Text in a Dialect

Debian: `filters'
WWW: http://www.princeton.edu/~mkporwit/pub_links/davido/slang/
WWW: http://www.mathlab.sunysb.edu/~elijah/src.html

There are all kinds of tools that work as filters on text; this recipe describes a specific group of filters -- those that filter their standard input to give the text an accent or dialect, and are intended to be humorous.

Generally speaking, a filter is a tool that works on standard input, changing it in some way, and then passing it to standard output.

These commands pass the contents of the file `/etc/motd' to the kraut filter, whose output is then sent to standard output. The contents of `/etc/motd' are not changed.

Some of the dialect filters available include nyc, which gives a "New Yawker" dialect to text, and newspeak, which translates text into the approved language of the thought police, as described in George Orwell's novel, 1984. Hail Big Brother!

Streaming Text

WWW: http://www.maurer-it.com/open-source/sview/

It's been demonstrated that people read and comprehend printed text faster than they read and comprehend text displayed on a computer display screen. Rapid serial visual presentation, or RSVP, is a technique that aims to increase reading speed and comprehension with the use of computer display screens. With this technique, text is displayed streamed on the screen, one word at a time, with pauses between words and punctuation. The average reading time is lowered and comprehension is increased significantly with this technique.

GNOME sview is a "streaming viewer" for X; it streams text a word at a time on the screen, at a default rate of 450 words per minute. Use it to read text files and the X selection, which is text you have selected with the mouse (see Selecting Text).

To open a file in sview, either specify it as an argument to the command, or choose Open from the File menu in sview, and select the file from there.

To start streaming the text, either press [S] once, or left-click on the button marked RSVP. Both [S] and the RSVP button toggle the streaming; the left and right arrow keys control the speed.

Text being streamed with sview looks like this:


The large area with the word `beginning' in it is where the text is being streamed. The text in the lower-left window is a shrunken view of the entire file, the text in the lower-right window is the paragraph from which the current word comes from.

To open another file, choose it from the menu; you can have many files open in sview at once. sview places each file in its own buffer. You can also paste the X selection into a buffer of its own -- to switch to a different buffer, choose its name from the Buffer menu.

Type [Q] to quit reading and exit sview.

The following table lists the keyboard commands used in sview and describes their meaning.
[] Decrease the stream speed.
[] Increase the stream speed.
C-o Open a file.
C-q Quit viewing text and exit sview.
C-w Erase the current text buffer.
M-n Move forward to the next word.
M-p Move backward to the previous word.
[S] Toggle the streaming of text.
[X] Display the X selection in its own buffer.
[N] Move forward to the next paragraph.
[P] Move backward to the previous paragraph.

Viewing a Character Chart

To view a character chart containing a list of all the valid characters in the ASCII character set and the character codes to use to type them, view the ascii man page.

You can use the octal codes listed for each character to type them in Emacs -- see Inserting Special Characters in Emacs.

The default Linux character set, the ISO 8859-1 ("Latin 1") character set, contains all of the standard ASCII character set plus an additional 128 characters.

To view the ISO 8859-1 character set, which contains an extended set of characters above the standard 127 ASCII characters, view the iso_8859_1 man page.

You can use this page to see all of the characters in this character set and how to input them.

NOTE: There's a special way to "quote" these characters in Emacs; this technique is described in Inserting Special Characters in Emacs.

The `miscfiles' package also contains charts for these character sets, as explained in Word Lists and Reference Files.

[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]