# less — Browse Files in the Terminal with the Pager

> Practical guide to less — page through, search and live-follow files and logs in the terminal with the standard Unix pager.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/less/

<!-- PROSE:intro -->
less is the standard pager on Unix and Linux: it shows file contents one screen at a time without loading the whole file into memory, and lets you scroll forward and backward, search and filter. Unlike `cat`, less loads on demand – ideal for huge log files, long output or reading over a pipe. It is also the default pager for `man` pages and `git diff`, so you meet it constantly even without invoking it directly. Because less only reads and never modifies anything, it is harmless; this guide walks you through the key bindings and options that let you move around quickly and precisely.
<!-- PROSE:intro:end -->

## Opening Files

`less <file>` — Open a file for browsing.

```bash
less /var/log/syslog
```

`less <file1> <file2>` — Open multiple files. Navigate between them with :n and :p.

```bash
less error.log access.log
```

`<command> | less` — Pipe command output into less for browsing.

```bash
journalctl | less
```

`less -N <file>` — Show line numbers in the left margin.

```bash
less -N script.py
```

`less -S <file>` — Chop long lines instead of wrapping. Scroll horizontally with arrow keys.

```bash
less -S wide-table.csv
```

`less -R <file>` — Show ANSI color codes (raw control characters). Essential for colored output.

```bash
git diff --color=always | less -R
```

`less +F <file>` — Open in follow mode (like tail -f). Press Ctrl+C to stop, Shift+F to resume.

```bash
less +F /var/log/syslog
```

`less +G <file>` — Open and immediately jump to the end of the file.

```bash
less +G /var/log/app.log
```

## Navigation

`Space / f` — Scroll forward one screen (page down).

```bash
Space or f
```

`b` — Scroll backward one screen (page up).

```bash
b
```

`d` — Scroll forward half a screen.

```bash
d
```

`u` — Scroll backward half a screen.

```bash
u
```

`j / Down / Enter` — Scroll forward one line.

```bash
j or Enter
```

`k / Up` — Scroll backward one line.

```bash
k or Up arrow
```

`g` — Jump to the beginning of the file.

```bash
g
```

`G` — Jump to the end of the file.

```bash
G
```

`<number>g` — Jump to a specific line number.

```bash
100g  (jump to line 100)
```

`<number>%` — Jump to a percentage position in the file.

```bash
50%  (jump to the middle)
```

`Right / Left` — Scroll horizontally when long lines are chopped (-S mode).

```bash
Right or Left arrow
```

## Searching

`/<pattern>` — Search forward for a pattern (regex supported).

```bash
/ERROR
```

`?<pattern>` — Search backward for a pattern.

```bash
?WARNING
```

`n` — Jump to the next search match.

```bash
n
```

`N` — Jump to the previous search match.

```bash
N
```

`&<pattern>` — Show only lines matching a pattern (like grep within less).

```bash
&ERROR
```

`&!<pattern>` — Show only lines NOT matching a pattern (inverse filter).

```bash
&!DEBUG
```

`&` — Clear the filter and show all lines again.

```bash
& (then Enter)
```

`/<pattern> with -i` — Case-insensitive search. Toggle with -i while inside less.

```bash
-i then /error
```

`Esc-u` — Toggle search highlight on/off.

```bash
Esc then u
```

## Marks & Bookmarks

`m<letter>` — Set a mark (bookmark) at the current position. Use any lowercase letter.

```bash
ma  (set mark 'a')
```

`'<letter>` — Jump to a previously set mark.

```bash
'a  (jump to mark 'a')
```

`''` — Jump back to the previous position (before the last jump).

```bash
''
```

## Multiple Files

`:n` — Open the next file when viewing multiple files.

```bash
:n
```

`:p` — Open the previous file.

```bash
:p
```

`:e <file>` — Open a new file without leaving less.

```bash
:e /var/log/error.log
```

`:x` — Show the list of currently opened files.

```bash
:x
```

## Display Options (Toggle at Runtime)

`-N` — Toggle line numbers on/off while viewing.

```bash
-N (then Enter)
```

`-S` — Toggle line wrapping/chopping while viewing.

```bash
-S (then Enter)
```

`-i` — Toggle case-insensitive search.

```bash
-i (then Enter)
```

`-I` — Toggle case-insensitive search and pattern matching.

```bash
-I (then Enter)
```

`-w` — Toggle highlighting of the first new line after a forward scroll.

```bash
-w (then Enter)
```

`=` — Show file information: filename, line numbers, byte position, percentage.

```bash
=
```

`v` — Open the current file in the default editor ($VISUAL or $EDITOR).

```bash
v
```

## Follow Mode

`F` — Enter follow mode (like tail -f). Waits for new data appended to the file.

```bash
F (while viewing a log file)
```

`Ctrl+C` — Exit follow mode and return to normal navigation.

```bash
Ctrl+C (while in follow mode)
```

`less +F <file>` — Open directly in follow mode from the command line.

```bash
less +F /var/log/syslog
```

## Startup Options

`less -N <file>` — Show line numbers from the start.

```bash
less -N config.py
```

`less -S <file>` — Chop long lines from the start (no wrapping).

```bash
less -S data.csv
```

`less -R <file>` — Interpret ANSI color escape sequences.

```bash
git log --color=always | less -R
```

`less -X <file>` — Don't clear the screen when exiting. Output remains visible in terminal.

```bash
less -X notes.txt
```

`less -F <file>` — Quit immediately if the entire file fits on one screen.

```bash
less -F short-file.txt
```

`less -RFXN <file>` — Common combination: color support, auto-quit for short files, keep output, line numbers.

```bash
less -RFXN script.py
```

`less -p '<pattern>' <file>` — Open the file and jump to the first occurrence of a pattern.

```bash
less -p 'function main' app.py
```

`less +/<pattern> <file>` — Alternative syntax to open with a search pattern.

```bash
less +/ERROR /var/log/syslog
```

## Environment & Configuration

`export LESS='-RFX'` — Set default less options via the LESS environment variable.

```bash
export LESS='-RFX'  # in ~/.bashrc
```

`export LESSOPEN='| highlight -O ansi %s'` — Configure an input preprocessor for syntax highlighting.

```bash
export LESSOPEN='| highlight -O ansi %s 2>/dev/null'
```

`export PAGER=less` — Set less as the default pager for man, git, and other tools.

```bash
export PAGER=less  # in ~/.bashrc
```

## Exiting & Help

`q` — Quit less.

```bash
q
```

`Q` — Quit less immediately (same as q, but guaranteed even in follow mode).

```bash
Q
```

`h` — Show the built-in help screen with all key bindings.

```bash
h
```

<!-- PROSE:outro -->
## Conclusion

less is the go-to tool for reading files and long output without changing them: you scroll forward and backward, jump to specific lines, search with regular expressions and filter the display – all memory-friendly, because less only loads on demand. Especially handy for logs is follow mode `+F` (like `tail -f`), which you pause with Ctrl+C and resume with Shift+F. For everyday use it is worth setting `less -RFX` as the default (for example via the `LESS` environment variable) so that colours render correctly and short files don't needlessly page away the output. Press `q` to leave the pager, and `h` to bring up the full key reference at any time.

## Further Reading

- [less(1) manual page](https://man7.org/linux/man-pages/man1/less.1.html) – the complete official reference for all options and key bindings
- [GNU less home page](https://www.greenwoodsoftware.com/less/) – the project home page with downloads and release notes
<!-- PROSE:outro:end -->

## Related Commands

- [vim](https://www.jpkc.com/db/en/cheatsheets/files-text/vim/) – the editor you switch to from less by pressing v
- [nano](https://www.jpkc.com/db/en/cheatsheets/files-text/nano/) – simple editor for editing the file you are reading
- [tail](https://www.jpkc.com/db/en/cheatsheets/files-text/tail/) – shows the end of files and follows logs with -f

