$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Paging with less: Search, Marks & Follow Mode

5 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

Why a pager exists

A terminal shows a fixed number of rows, and a file can have millions. A pager solves the problem by reading the input on demand and showing one screenful at a time. It keeps only what it needs in memory, and it can jump to any part of a file by seeking, when the input is a regular file. That is why less opens a huge file at once, while a program that reads the whole file first would make you wait.

When the output of a command is sent to a pipe, less reads from the pipe, and it caches what it has read so that you can scroll backward. It cannot seek in a pipe, so jumping to the end of a long stream means reading all of it first. This difference is the reason some operations are instant on a file and slow on a stream.

BASH3 lines
cd /workspace
seq 1 500 > long.txt
less long.txt

Inside the pager, press q to quit. Space moves one page forward, and b moves one page back. The lab has no mouse, so the keys are the way to move.

Moving around

The arrow keys and the Page Up and Page Down keys work, but the letter keys are faster once you know them. The key g goes to the first line and G goes to the last. A count before a motion repeats it, so 50j moves down fifty lines. A colon followed by a number, such as :100 or the older form of a number followed by g, jumps to that line. Percent moves to a point in the file by position, which is useful for a long file where you only know roughly where you want to be.

Searching

Type a slash and a pattern to search forward, or a question mark to search backward. The pattern is a regular expression, so /error[0-9] finds the word error followed by a digit. The keys n and N repeat the search in the same or the opposite direction. The matches are highlighted when the terminal supports it, and pressing Escape followed by u clears the highlight without changing the search.

Searching is incremental in the sense that less reads more of the file as needed. A search through a huge file on a regular file is fast because the pager can seek, but a search through a slow stream may pause while the input is read.

Marks: remembering places

Press m followed by a letter to set a mark at the current top line. Press an apostrophe followed by the same letter to return to that line. Marks are a small convenience, and they make it easy to compare two distant parts of a long file. Lowercase letters are per session, and they disappear when you quit.

Follow mode

Press F to enter follow mode. It works like tail -f: less waits for new output at the end of the file and displays it as it appears. Press Ctrl-C to stop following and return to normal paging, and press q to leave less. Follow mode is the reason many people use less for logs, because they can scroll back to read context and then follow again.

Note

A pager is interactive, so it needs a terminal on standard output. When standard output is not a terminal, less behaves like cat and copies the input through. That behavior is useful in scripts, and it explains why a command that works in the terminal can print the whole file when you redirect it.

Useful options

The option -N numbers the lines in the display, -S chops long lines instead of wrapping them, and -X leaves the screen contents visible after you quit. The option -F quits at once if the whole file fits on one screen. You can set these in the environment variable LESS so they apply to every invocation, which is a common setup in a shell profile.

To read the version of the pager in the lab, use the --version option. It prints one line starting with the word less and the release number. This is a quick way to see which features are available, since older versions lack some of them.

Reading long help text in a pager

Many programs print long help text to standard output, and a pager is the right place to read it. Pipe the output into less, as in ls --help | less, and search the text with a slash. This works with any command that prints help, including the shell builtins that bash provides through its help command. The pager does not need a file, because it reads from the pipe as the program writes.

Piping help into a pager is a practical substitute for a manual on a system that lacks man pages. It gives you the same search and navigation as a manual, and the text is the version that the installed program actually uses. Check the version first if the output seems to describe options you do not have.

If you set the variable LESS in your shell profile, every invocation of less uses those options. A common choice is to add -R so that color codes are shown, and -F so that short output is printed without starting the pager. Choose options that make short output behave like cat, because scripts and pipelines should not stop at a prompt.

Practice in the lab

The lab cannot drive an interactive pager, so it asks you to use less in the ways that finish without a keyboard. You will read its version, and you will run it on a file whose output is redirected. Notice that the output is the same as the input, because a pager that is not attached to a terminal copies its input.

Terminal lab · 2 steps · real Linux in your browser

Use less without a keyboard

Read the pager version, and run less on a pipe-safe file so its output can be captured.

01
Read the pager version
Save the first line of the version output of less: ``bash less --version | head -n 1 > less-version.txt ``
Show hint for step 1
less --version prints a line that starts with the word less.
02
Run less with output redirected
less copies its input when its output is not a terminal. Run it on small.txt with the output saved: ``bash less small.txt > piped.txt ``
Show hint for step 2
The output is redirected, so less does not start its interactive screen.
Check yourself Question 1 of 4
Concept Check Searching forward recall

Which key starts a forward search in less?

Recap & Key Takeaways

less reads its input on demand, so it can open a huge file quickly, though it cannot seek within a pipe.
Press / to search with a regular expression, and use n and N to repeat it.
Press m followed by a letter to set a mark, and an apostrophe followed by the letter to return to it.
Press F to follow a file as it grows, and Ctrl-C to stop following and return to paging.

Primary Sources & Further Reading

Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue