Skip to content

less Command Cheat Sheet

less is a terminal pager program equivalent to more, but with vastly more features. It allows backward navigation and does not load the entire file into memory, making it instant for gigabyte-sized logs.


Synopsis

less [OPTIONS] [FILE]

Key Action
Space / f Scroll forward one page
b Scroll backward one page
d Scroll forward half page
u Scroll backward half page
Enter / j Scroll forward one line
k Scroll backward one line
G Go to End of file
g Go to Start of file
50g Go to Line 50
10% Go to 10% of file

Searching

Type / followed by a regex pattern.

/Error
- n: Next occurrence. - N: Previous occurrence.

Type ? followed by a regex pattern.

?Start

Highlight Matches

Toggle highlighting matches with Esc then u.


Power User Features

Follow Mode (Shift+F)

Behave like tail -f. Press Shift+F inside less. - New data appearing in the file will scroll in. - Press Ctrl+C to stop following and return to normal navigation.

Marking Positions

Save a spot to return to later. 1. Press m followed by a letter (e.g., a). -> "Mark set" 2. Scroll away. 3. Press ' followed by a. -> Jumps back to mark a.

Multiple Files

Open multiple files: less file1.log file2.log - :n : Next file - :p : Previous file


Command Line Options

Show Line Numbers (-N)

less -N filename

Quit if One Screen (-F)

If the file fits on one screen, just print it and exit (like cat).

less -F filename

Raw Control Characters (-R)

Useful for viewing colored output (e.g., from grep --color or ls --color).

ls -l --color | less -R

Case Insensitive Search (-I)

less -I filename

Environment Variables

LESS

Set default options for less. Add to ~/.bashrc:

export LESS="-R -i -N"
Now less will always show colors, be case-insensitive, and show line numbers.

PAGER

Set less as the default pager for man, git, etc.

export PAGER=less

Input Preprocessor (lesspipe)

less can handle binary files (zip, tar, pdf) by using an input preprocessor. Most distros set LESSOPEN automatically.

less archive.zip
(Shows the list of files inside the zip).

less document.pdf
(Displays text content of PDF).


Command Palette inside less

Press h inside less to verify these shortcuts.

  • !cmd : Run a shell command.
  • v : Edit current file in $EDITOR (e.g., vim). !!! Super useful.

Notes

  • Search: less uses standard regex. .* works as expected.
  • Pipe: cat large.log | less allows scrolling, but you lose the ability to see the file progress (%) because length is unknown.