Preserving Terminal Contents When Exiting less with the -X Option
Learn how to use less -X to view files without clearing the terminal, including prerequisites, steps, checks, and limitations.
23 May 2026, 15:58 UTC

Desired outcome
You want to view a text file with less but keep the terminal’s existing contents visible after you quit the pager. This is useful when you need to reference command output or configuration while scrolling through a file, without the screen being cleared or restored to an alternate buffer.
Prerequisites
- A Unix‑like environment (Linux, macOS, *BSD) with
lessinstalled. Most systems shiplessas part of the base utilities. - A readable text file (e.g.,
/etc/passwdor a log file) that you intend to inspect. - A terminal that understands termcap/terminfo – virtually all modern terminals (xterm, GNOME Terminal, iTerm2, Windows Terminal, etc.) meet this requirement.
- Permission to read the target file; no special privileges are needed unless the file itself is restricted.
Procedure
- Open your terminal emulator.
- Run
lesswith the-Xflag followed by the path to the file you want to view:less -X /path/to/your/file.txtThe
-Xoption tellslessnot to send the terminal’s initialization and de‑initialization strings, which are responsible for switching to the alternate screen buffer and clearing it on exit. - Navigate inside
lessas usual: Space or f for forward page, b for backward page, arrow keys, g for top, G for bottom, etc. - When you are finished, press q to quit
less. Because the alternate screen was never activated, the terminal returns to exactly the same state it had before the command started.
Expected checks
- Exit status: Immediately after quitting, run
echo $?. A value of0indicates thatlessterminated successfully. - Visual verification: Compare the terminal’s content before launching
less -Xwith what you see after quitting. Aside from the new command prompt, the preceding output should be unchanged. - Prompt and line numbers: If you used additional flags (e.g.,
-Nto show line numbers) they remain active while insidelessbut do not persist after you quit, which is expected behavior.
Limitations and practical verification
The -X flag disables the alternate screen buffer. Consequently:
- Programs that rely on that buffer (such as
vim,nano, or evenlessitself when invoked from another program) may leave visual artifacts if they also use-X. For example, runningvim -c 'set lazyredraw'inside aless -Xsession could leave the editor’s UI partially visible after you quit both programs. - Very old versions of
less(prior to 3.0) may not recognize-X. In those cases the flag is either ignored or causes an error. Verify your version withless --version; the output should show a version number of 3.0 or higher.
To confirm that the alternate screen is indeed disabled, you can invoke the built‑in editor from within less:
- Start
less -X file.txt. - Press v (the default key to launch the editor defined by
$VISUALor$EDITOR). - The editor should open in the same screen without clearing the terminal. After you exit the editor, you will return to
lessand then to the original terminal state upon quittinglesswith q.
Recovery options
Using -X does not modify system state, files, or terminal settings beyond the display behavior during the current command. Therefore no rollback procedure is required. If you encounter unwanted visual artifacts because a child program also used -X, simply clear the terminal with Ctrl+L (or run reset) to restore a clean state.
Summary
The -X option for less is a straightforward way to keep your terminal’s contents visible after you finish browsing a file. It works on any recent Unix‑like system with a standard terminal, requires no special permissions, and leaves no lasting changes to the environment. Verify success by checking the exit code and confirming that the screen looks the same before and after the command.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.