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


Unix Power ToolsUnix Power ToolsSearch this book

12.3. "less" is More

The most popular pager for Unix systems was once the more command, so named because it gave you "one more screen." more is ubiquitous, but also somewhat limited in its capability. The less command (so named because, of course, "less is more!") is more commonly used. less is a full-featured text pager that emulates more but offers an extended set of capabilities.

One particularly important feature of less is that it does not read all of its input before starting, which makes it faster than an editor for large input. less also offers many useful features and is available for almost every operating environment. As an extra bonus, it is installed by default on most free Unixes.

less begins execution by first examining the environment in which it is running. It needs to know some things about the terminal (or window) in which its output will be displayed. Once that's known, less formats the text and displays the first screen's output. The last line of the screen is reserved for user interaction with the program. less will display a colon (:) on the first column of the last line and leave the cursor there. This colon is a command prompt, awaiting command input from the user. Most commands to less are single-character entries, and less will act upon them immediately and without a subsequent carriage return (this is known as cbreak mode). The most basic command to less (and more) is a single space, which instructs the pager to move ahead in the text by one screen. Table 12-1 lists commonly used less commands.

Table 12-1. Commonly used less commands

Command

Description

Space

Scroll forward one screen.

d

Scroll forward one-half screen.

RETURN

Scroll forward one line.

b

Scroll backward one screen. Unlike more, while less is reading from pipes (Section 1.5), it can redraw the screen and read previous pages.

u

Scroll backward one-half screen.

y

Scroll backward one line.

g

Go to the beginning of the text (could be slow with large amounts of text).

G

Go to the end of the text (could be slow with large amounts of text).

/pattern

Search forward for pattern, which can be a regular expression.

?pattern

Search backward for pattern, which can be a regular expression.

n

Search for the next occurance of the last search, in the same direction: forward in the file if the previous search was using / and backwards in the file if the previous search was using ?.

N

Search for the previous occurance of the last search. See earlier.

h

Display a help screen.

:n

Display next file from command line (two-character command).

:p

Display previous file from command line (two-character command).

less has a rich command set, and its behavior can be modified as needed for your use. The lesskey program lets you make custom key definitions, and you can store your favorite setup options in the LESS environment variable (Section 35.3). See the less manpage for further details.

One of the big advantages of less is that it doesn't require any relearning; less does the right thing when you use more, vi (Section 17.2), or emacs (Section 19.1) file-browsing commands. Incidentally, it also protects you from terminal control sequences and other obnoxious things that happen when you try to view a binary file, because it escapes nonprinting characters (Section 12.4).

-- JD



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.