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




Scroll forward one screen.


Scroll forward one-half screen.


Scroll forward one line.


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


Scroll backward one-half screen.


Scroll backward one line.


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


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


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


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


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


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


Display a help screen.


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


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.