Knowing how to use the ubiquitous shell pager “less” is an essential command line skill.

This page is intended as both an introduction to less and a quick reference for the “intermediate” commands and options I end up forgetting and relearning about once a year.

Written in the mid-1980s and still maintained today by the same author, less has been ported to numerous operating systems and is the default pager on most Unix-like platforms.

Most shell users are likely to encounter less while viewing manual pages with the man command.

Manual page for for less (displayed in less)

Because it doesn’t need to load the entire file before starting, less is also particularly well suited for examining large text files.

Basic Usage

These are some of the basic less commands and options, not a complete list.

Help – Press h for help.
“If you forget all the other commands, remember this one.” – LESS Manual

Movement  
f or space forward 1 window
b back 1 window
j or forward 1 line
y or back 1 line
g go to first line
G go to last line
number then g jump (go) to line number
Horizontal scrolling  
- scroll horizontally by half a screen
{ moves left to first column
} moves right to last column displayed
Marks  
mletter “m” + any letter marks a position
'letter apostrophe + letter returns to that mark
mletter clear mark

Searching

/pattern search forward
?pattern search backward
n find next
N find previous
u toggle highlighting

The search pattern is a regular expression.

Options

Most of the option flags that can applied from the command line can also toggled on and off while less is running by pressing “–” (hyphen) followed by the appropriate letter.

For example, to toggle line wrapping while less is running, press then S.

Search options  
-g highlight last match only
-i ignore case if not uppercase
-I ignore case
-J mark matching lines in vertical column
Display options  
-m medium prompt
-M long prompt
-n show line numbers or bytes
-N always show line numbers
-S toggle line wrapping
Startup options  
-e quit at end of file
-F quit if one screen
-R raw control chars (enable color)
-X no init (don’t clear screen)

Other features

The basic commands above barely scratch the surface. If you use less regularly, you’ll probably want to explore some of these features as well.

$LESS - defaults

Store preferred options in the variable LESS. For example, to have less have start with the -e (exit at end of file), -X (no init, i.e. don’t redraw screen), and -F (quit if file fits in one screen) options, add this to your shell configuration:

export LESS='-eFX'

Options specified in the command line override those stored in the LESS variable.

To reset an option set in the LESS variable to the default, call less with the option specified with -+:

less -+XF   # set -X and -F options to default

Working with files

These are the basic commands for working with multiple files in less:

Files  
:e file examine (load) file
:n next file
:p previous file
s file save piped content as file
v edit current file with $VISUAL or $EDITOR

There are many additional features for working for multiple files. See the manual page for more information.

Shell commands

! shell-command run shell command
| mark-letter shell-command pipe input to shell command

The | command pipes input from less into a shell command. For example, to save an entire man page to a text file (piping it through col -b to remove terminal formatting):

Press g to jump to the beginning and | to start the pipe command. Enter $ (end of document) as the mark. Then enter col -b > filepath as the shell command.

Reference

Cheatsheet
This LESS cheatsheet (gist) includes the basic commands and options shown here as well as a few others.
Official Website
http://www.greenwoodsoftware.com/less/

Tags:

Categories:

Published: