On Unix-like operating systems, the less command is a feature-rich command-line file viewer.

This page covers the GNU/Linux version of less.

Description

less is a program similar to more, but it has many more features. less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi. less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret ("^").

  • Description
  • Syntax
  • Examples
  • Related commands
  • Linux commands help

Commands are based on both more and vi. Commands may be preceded by a decimal number, called N in the descriptions below. The number is used by some commands, as indicated.

Syntax

less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~] [-b space] [-h lines] [-j line] [-k keyfile] [-{oO} logfile] [-p pattern] [-P prompt] [-t tag] [-T tagsfile] [-x tab,…] [-y lines] [-[z] lines] [-# shift] [+[+]cmd] [–] [filename]…

Commands

In the following descriptions, ^X means control-X. Esc stands for the Escape key; for example Esc-v means the two character sequence “Escape”, then “v”.

The following four commands may or may not be valid, depending on your particular installation.

Options

Command line options are described below. Most options may be changed while less is running, via the “-” command. Most options may be given in one of two forms: either a dash followed by a single letter, or two dashes followed by a long option name. A long option name may be abbreviated as long as the abbreviation is unambiguous. For example, –quit-at-eof may be abbreviated –quit, but not –qui, since both –quit-at-eof and –quiet begin with –qui. Some long option names are in uppercase, such as –QUIT-AT-EOF, as distinct from –quit-at-eof. Such option names need only have their first letter capitalized; the remainder of the name may be in either case. For example, –Quit-at-eof is equivalent to –QUIT-AT-EOF. Options are also taken from the environment variable “LESS”. For example, to avoid typing “less -options …” each time less is invoked, you might tell csh:

setenv LESS “-options”

or if you use sh:

LESS="-options"; export LESS

On MS-DOS, you don’t need the quotes, but you should replace any percent signs in the options string by double percent signs.

The environment variable is parsed before the command line, so command line options override the LESS environment variable. If an option appears in the LESS variable, it can be reset to its default value on the command line by beginning the command line option with “-+”.

Some options like -k or -D require a string to follow the option letter. The string for that option is considered to end when a dollar sign ($) is found. For example, you can set two -D options on MS-DOS like this:

LESS=“Dn9.1$Ds4.1”

A dollar sign or backslash may be included literally in an option string by preceding it with a backslash.

Line Editing

When entering command line at the bottom of the screen (for example, a filename for the :e command, or the pattern for a search command), certain keys can be used to manipulate the command line. Most commands have an alternate form, listed below in brackets (for example, KEY-COMBO [ ALTERNATE-KEY-COMBO ] ) which can be used if a key does not exist on a particular keyboard. (Note that the forms beginning with ESC do not work in some MS-DOS and Windows systems because ESC is the line erase character.) Any of these special keys may be entered literally by preceding it with the “literal” character, either ^V or ^A. A backslash itself may also be entered literally by entering two backslashes.

ESC [ … m

Key Bindings

You may define less commands using the program lesskey to create a lesskey file. This file specifies a set of command keys and an action associated with each key. You may also use lesskey to change the line-editing keys (see LINE EDITING), and to set environment variables. If the environment variable LESSKEY is set, less uses that as the name of the lesskey file. Otherwise, less looks in a standard place for the lesskey file: On Unix systems, less looks for a lesskey file called “$HOME/.less”. On MS-DOS and Windows systems, less looks for a lesskey file called “$HOME/_less”, and if it is not found there, then looks for a lesskey file called “_less” in any directory specified in the PATH environment variable. On OS/2 systems, less looks for a lesskey file called “$HOME/less.ini”, and if it is not found, it looks for a lesskey file called “less.ini” in any directory specified in the INIT environment variable, and if it not found there, it looks for a lesskey file called “less.ini” in any directory specified in the PATH environment variable.

A system-wide lesskey file may also be set up to provide key bindings. If a key is defined in both a local lesskey file and in the system-wide file, key bindings in the local file take precedence over those in the system-wide file. If the environment variable LESSKEY_SYSTEM is set, less uses that as the name of the system-wide lesskey file. Otherwise, less looks in a standard place for the system-wide lesskey file: On Unix systems, the system-wide lesskey file is /usr/local/etc/sysless. (However, if less was built with a different sysconf directory than /usr/local/etc, that directory is where the sysless file is found.) On MS-DOS and Windows systems, the system-wide lesskey file is c:_sysless. On OS/2 systems, the system-wide lesskey file is c:\sysless.ini.

Input Preprocessor

You may define an “input preprocessor” for less. Before less opens a file, it first gives your input preprocessor a chance to modify the way the contents of the file are displayed. An input preprocessor is an executable program (or shell script), which writes the contents of the file to a different file, called the replacement file. The contents of the replacement file are then displayed in place of the contents of the original file. However, it will appear to the user as if the original file is opened; that is, less displays the original filename as the name of the current file.

An input preprocessor receives one command line argument, the original filename, as entered by the user. It should create the replacement file, and when finished, print the name of the replacement file to its standard output. If the input preprocessor does not output a replacement filename, less uses the original file, as normal. The input preprocessor is not called when viewing standard input. To set up an input preprocessor, set the LESSOPEN environment variable to a command line which will invoke your input preprocessor. This command line should include one occurrence of the string “%s”, which will be replaced by the filename when the input preprocessor command is invoked.

When less closes a file opened in such a way, it will call another program, called the input postprocessor, which may perform any desired clean-up action (such as deleting the replacement file created by LESSOPEN). This program receives two command line arguments, the original filename as entered by the user, and the name of the replacement file. To set up an input postprocessor, set the LESSCLOSE environment variable to a command line which will invoke your input postprocessor. It may include two occurrences of the string “%s”; the first is replaced with the original name of the file and the second with the name of the replacement file, which was output by LESSOPEN.

For example, on many Unix systems, these two scripts will allow you to keep files in compressed format, but still letlessview them directly:

lessopen.sh:

#! /bin/sh case “$1” in *.Z) uncompress - if [ -s /tmp/less.$$ ]; then echo /tmp/less.$$ else rm -f /tmp/less.$$ fi ;; esac

lessclose.sh:

#! /bin/sh rm $2

To use these scripts, put them both where they can be executed and set LESSOPEN=“lessopen.sh %s”, and LESSCLOSE=“lessclose.sh %s %s”. More complex LESSOPEN and LESSCLOSE scripts may be written to accept other types of compressed files, and so on.

It is also possible to set up an input preprocessor to pipe the file data directly to less, rather than putting the data into a replacement file. This avoids the need to decompress the entire file before starting to view it. An input preprocessor that works this way is called an input pipe. An input pipe, instead of writing the name of a replacement file on its standard output, writes the entire contents of the replacement file on its standard output. If the input pipe does not write any characters on its standard output, then there is no replacement file and less uses the original file, as normal. To use an input pipe, make the first character in the LESSOPEN environment variable a vertical bar (|) to signify that the input preprocessor is an input pipe.

For example, on many Unix systems, this script will work like the previous example scripts:

lesspipe.sh:

#! /bin/sh case “$1” in *.Z) uncompress -c $1 2>/dev/null *) exit 1 ;; esac exit $?

To use this script, put it where it can be executed and set LESSOPEN="|lesspipe.sh %s".

Note that a preprocessor cannot output an empty file, since that is interpreted as meaning there is no replacement, and the original file is used. To avoid this, if LESSOPEN starts with two vertical bars, the exit status of the script becomes meaningful. If the exit status is zero, the output is considered to be replacement text, even if it empty. If the exit status is nonzero, any output is ignored and the original file is used. For compatibility with previous versions of less, if LESSOPEN starts with only one vertical bar, the exit status of the preprocessor is ignored.

When an input pipe is used, a LESSCLOSE postprocessor can be used, but it is usually not necessary since there is no replacement file to clean up. In this case, the replacement file name passed to the LESSCLOSE postprocessor is “-”.

For compatibility with previous versions of less, the input preprocessor or pipe is not used if less is viewing standard input. However, if the first character of LESSOPEN is a dash (-), the input preprocessor is used on standard input as well as other files. In this case, the dash is not considered to be part of the preprocessor command. If standard input is being viewed, the input preprocessor is passed a file name consisting of a single dash. Similarly, if the first two characters of LESSOPEN are vertical bar and dash (|-) or two vertical bars and a dash (||-), the input pipe is used on standard input as well as other files. Again, in this case the dash is not considered to be part of the input pipe command.

National Character Sets

There are three types of characters in the input file:

  • normal characters can be displayed directly to the screen.
  • control characters should not be displayed directly, but are expected to be found in ordinary text files (such as backspace and tab).
  • binary characters should not be displayed directly and are not expected to be found in text files.

A “character set” is a description of which characters are to be considered normal, control, and binary. The LESSCHARSET environment variable may be used to select a character set. Possible values for LESSCHARSET are:

In rare cases, it may be desired to tailor less to use a character set other than the ones definable by LESSCHARSET. In this case, the environment variable LESSCHARDEF can be used to define a character set. It should be set to a string where each character in the string represents one character in the character set. The character “.” is used for a normal character, “c” for control, and “b” for binary. A decimal number may be used for repetition. For example, “bccc4b.” would mean character 0 is binary, 1, 2 and 3 are control, 4, 5, 6 and 7 are binary, and 8 is normal. All characters after the last are taken to be the same as the last, so characters 9 through 255 would be normal. (This is an example, and does not necessarily represent any real character set.)

This table shows the value of LESSCHARDEF which is equivalent to each of the possible values for LESSCHARSET:

If neither LESSCHARSET nor LESSCHARDEF is set, but any of the strings “UTF-8”, “UTF8”, “utf-8” or “utf8” is found in the LC_ALL, LC_CTYPE or LANG environment variables, then the default character set is utf-8.

If that string is not found, but your system supports the setlocale interface, less will use setlocale to determine the character set. setlocale is controlled by setting the LANG or LC_CTYPE environment variables.

Finally, if the setlocale interface is also not available, the default character set is latin1.

Control and binary characters are displayed in standout (reverse video). Each such character is displayed in caret notation if possible (e.g., ^A for control-A). Caret notation is used only if inverting the 0100 bit results in a normal printable character. Otherwise, the character is displayed as a hex number in angle brackets. This format can be changed by setting the LESSBINFMT environment variable. LESSBINFMT may begin with a “*” and one character to select the display attribute: “*k” is blinking, “*d” is bold, “*u” is underlined, “*s” is standout, and “n” is normal. If LESSBINFMT does not begin with a “”, normal attribute is assumed. The remainder of LESSBINFMT is a string which may include one printf-style escape sequence (a % followed by x, X, o, d, etc.). For example, if LESSBINFMT is “*u[%x]”, binary characters are displayed in underlined hexadecimal surrounded by brackets. The default if no LESSBINFMT is specified is “*s<%02X>”. Warning: the result of expanding the character via LESSBINFMT must be less than 31 characters.

When the character set is utf-8, the LESSUTFBINFMT environment variable acts similarly to LESSBINFMT but it applies to Unicode code points that were successfully decoded but are unsuitable for display (e.g., unassigned code points). Its default value is “<U+%04lX>”. Note that LESSUTFBINFMT and LESSBINFMT share their display attribute setting ("*x") so specifying one will affect both; LESSUTFBINFMT is read after LESSBINFMT so its setting, if any, will have priority. Problematic octets in a UTF-8 file (octets of a truncated sequence, octets of a complete but non-shortest form sequence, illegal octets, and stray trailing octets) are displayed individually using LESSBINFMT so as to facilitate diagnostics of how the UTF-8 file is ill-formed.

Prompts

The -P option allows you to tailor the prompt to your preference. The string given to the -P option replaces the specified prompt string. Certain characters in the string are interpreted specially. The prompt mechanism is rather complicated to provide flexibility, but the ordinary user need not understand the details of constructing personalized prompt strings.

A percent sign followed by a single character is expanded according to what the following character is:

If any item is unknown (for example, the file size if input is a pipe), a question mark is printed instead.

The format of the prompt string can be changed depending on certain conditions. A question mark followed by a single character acts like an “IF”: depending on the following character, a condition is evaluated. If the condition is true, any characters following the question mark and condition character, up to a period, are included in the prompt. If the condition is false, such characters are not included. A colon appearing between the question mark and the period can be used to establish an “ELSE”: any characters between the colon and the period are included in the string if and only if the IF condition is false. Condition characters (which follow a question mark) may be:

Any characters other than the special ones (question mark, colon, period, percent, and backslash) become literally part of the prompt. Any of the special characters may be included in the prompt literally by preceding it with a backslash.

Some examples:

?f%f:Standard input.

This prompt prints the filename, if known; otherwise the string “Standard input.”.

?f%f .?ltLine %lt:?pt%pt%:?btByte %bt:-…

This prompt would print the filename, if known. The filename is followed by the line number, if known, otherwise the percent if known, otherwise the byte offset if known. Otherwise, a dash is printed. Notice how each question mark has a matching period, and how the % after the %pt is included literally by escaping it with a backslash.

?n?f%f .?m(file %i of %m) ..?e(End) ?x- Next: %x..%t

This prints the filename if this is the first prompt in a file, followed by the “file Nof N” message if there is more than one input file. Then, if we are at end-of-file, the string “(End)” is printed followed by the name of the next file, if there is one. Finally, any trailing spaces are truncated. This is the default prompt. For reference, here are the defaults for the other two prompts (-m and -M respectively). Each is broken into two lines here for readability only.

?n?f%f .?m(file %i of %m) ..?e(End) ?x- Next: %x.: ?pB%pB%:byte %bB?s/%s…%t ?f%f .?n?m(file %i of %m) ..?ltlines %lt-%lb?L/%L. : byte %bB?s/%s. .?e(End) ?x- Next: %x.:?pB%pB%..%t

And here is the default message produced by the = command:

?f%f .?m(file %i of %m) .?ltlines %lt-%lb?L/%L. . byte %bB?s/%s. ?e(End) :?pB%pB%..%t

The prompt expansion features are also used for another purpose: if an environment variable LESSEDIT is defined, it is used as the command to be executed when the v command is invoked. The LESSEDIT string is expanded in the same way as the prompt strings. The default value for LESSEDIT is:

%E ?lm+%lm. %f

Note that this expands to the editor name, followed by a + and the line number, followed by the file name. If your editor does not accept the “+linenumber” syntax, or has other differences in invocation syntax, the LESSEDIT variable can be changed to modify this default.

Security

When the environment variable LESSSECURE is set to 1, less runs in a “secure” mode. This means these features are disabled:

Less can also be compiled to be permanently in “secure” mode.

Compatibility With More

If the environment variable LESS_IS_MORE is set to 1, or if the program is invoked via a file link named “more”, less behaves (mostly) in conformance with the POSIX more command specification. In this mode, less behaves differently in these ways:

  • The -e option works differently. If the -e option is not set, less behaves as if the -E option were set. If the -e option is set,lessbehaves as if the -e and -F options were set.
  • The -m option works differently. If the -m option is not set, the medium prompt is used, and it is prefixed with the string “–More–”. If the -m option is set, the short prompt is used.
  • The -n option acts like the -z option. The normal behavior of the -n option is unavailable in this mode.

The parameter to the -p option is taken to bea lesscommand rather than a search pattern.

The LESS environment variable is ignored, and the MORE environment variable is used in its place.

Environment Variables

Examples

less file.txt

Views the file file.txt.

less -N file.txt

Views the file file.txt, displaying a line number at the beginning of each line.

more — Display text one screen at a time.