Last updated on December 25, 2024
Due to its speed and simplicity, GNU less
is probably the most common default terminal pager on various GNU/Linux distributions—you may have probably used it explicitly via the less
command, or implicitly when you execute the man
command or git diff
. Although the default configuration of less
does not really offer much except for a basic text viewer, it is actually much more powerful than most people think. Here a few improvements over the default configuration are offered.
For macOS/OS X users: consider installing a newer version of less
and other GNU command line utilities. To do so, you can follow the instructions here.
Better default GNU less
command line options
Add the following to your ~/.bashrc
if you use bash or ~/.zshrc
if you use zsh:
# set options for less
export LESS='--quit-if-one-screen --ignore-case --LONG-PROMPT --RAW-CONTROL-CHARS --HILITE-UNREAD --tabs=4 --no-init --window=-4'
# or the short version
# export LESS='-F -i -M -R -W -x4 -X -z-4'
--quit-if-one-screen
or-F
: Exit if the entire file can be displayed in one screen. This is usually desirable as there is no incentive to stay inless
if a single screen can display all the contents.--ignore-case
or-i
: Cause search inless
to ignore case unless an uppercase letter is present in the search pattern.--LONG-PROMPT
or-M
: Prompt more verbosely.--RAW-CONTROL-CHARS
or-R
: Cause ANSI "color" escape sequences to be displayed in their raw form. This is for the color display explained later in the next section.--HILITE-UNREAD
or-W
: Highlight the first unread line after scrolling the screen for more than one lines.--tabs=4
or-x4
: Display a tab as 4-character width, since most modern text files assume 4-character width for a tab. However, you can still change this to your preference if, for example, you frequently read Linux kernel source code which uses 8-character width for a tab.--no-init
or-X
: Disable sending the termcap initialization and deinitialization strings to the terminal. The purpose here is to prevent clearing screen after exiting. If you do not want this feature and want to remove it, please keep in mind that--quit-if-one-screen
also relies on this option to work properly. For a workaround, please check out here. Also note that this option also disables mouse scrolling.--window=-4
or-z-4
: Change the default scrolling size to 4 lines fewer than the current screen size, so always keep 4 lines overlapping with previous screen when scrolling with the space key.
I used to recommend --status-column
or -J
, which displays a status column on the left to indicate lines that match current search or indicate the first unread line after moving a full page. However, on some systems, it breaks the terminal width calculation of many programs, such as man
, various subcommands in git
. Feel free to give it a try and see if things work well.
Since this configuration makes less
use one more column, the man
command likely does not guess the width of the terminal correctly. To correct this, add the following to your shell configuration file:
alias man='MANWIDTH=$(expr $COLUMNS - 1) man'
A more colorful less
Add the following to your ~/.bashrc
if you use bash or ~/.zshrc
if you use zsh:
# Set colors for less. Borrowed from https://wiki.archlinux.org/index.php/Color_output_in_console#less .
export LESS_TERMCAP_mb=$'\E[1;31m' # begin bold
export LESS_TERMCAP_md=$'\E[1;36m' # begin blink
export LESS_TERMCAP_me=$'\E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'\E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'\E[0m' # reset reverse video
export LESS_TERMCAP_us=$'\E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'\E[0m' # reset underline
Now restart your shell and run man less
—the manual is in colors! The difference is shown in the following two images:

Before setting colors

After setting colors
Reading non-text files and highlighting syntax in less
Quite often we want to view some non-text files quickly in terminal, such as to view a gz or zip file without explicitly decompressing it. We also enjoy syntax highlighting when reading source code. To achieve these, less
provides an "input preprocessor" feature. This feature allows a preprocessor, usually named lesspipe{.sh}
, to process the file before less
displays it. This preprocessor can, for example, decompress compressed files, convert binary files to a human-readable format, or add syntax highlighting. There are many input preprocessors floating around on the Internet, but I strongly recommend lesspipe.sh by Wolfgang Friebel. In the rest of this section, we will install this script and update the configuration to make use of the script.
- Clone the repository with git (if git is not available on your computer, you can download the latest version here):
git clone https://github.com/wofr06/lesspipe.git cd lesspipe
- Run
./configure
to detect the availability of various preprocessing programs on your system. Follow the instructions to finish this program. Remember to answery
for the question “Activate syntax highlighting code?”. - Copy
lesspipe.sh
andcode2color
to one of your directories in thePATH
environmental variable, such as/usr/local/bin
:cp lesspipe.sh code2color /usr/local/bin
- Add the following to your
~/.bashrc
if you use bash or~/.zshrc
if you use zsh:# Set the Less input preprocessor. if type lesspipe.sh >/dev/null 2>&1; then export LESSOPEN='|lesspipe.sh %s' fi
This will set the
less
input preprocessor tolesspipe.sh
if it is found. - Optionally, to achieve better syntax highlighting when reading source code, install pygments and add the following to your
~/.bashrc
if you use bash or~/.zshrc
if you use zsh:if type pygmentize >/dev/null 2>&1; then export LESSCOLORIZER='pygmentize' fi
This code snippet sets
pygmentize
as the syntax highlighter rather than the built-in syntax highlighter incode2color
. However, using pygments also leads to a longer startup time ofless
.
Now you should be able to use less
to directly open various non-text files: *.gz
, *.xz
, *.doc
, etc. Also, syntax highlighting is enabled in less
:
Also check out the following links for further improving shell experience:
I put the following into my bash profile and I took out –LONG-PROMPT and –HILITE-UNREAD for personal preference.
If I remove it everything, the scroll works again. Add it back in, stops working.
I am NOT able to scroll when less first opens, however, if I use the spacebar or equivalent keyboard shortcuts, then I am able to scroll up and down for that exact amount of lines.
If I shortcut to the beginning of the less page, I can no longer scroll once again.
# set options for less
# https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
export LESS=’–quit-if-one-screen –ignore-case –status-column –RAW-CONTROL-CHARS –tabs=4 –no-init –window=-4′
# Set colors for less. Borrowed from https://wiki.archlinux.org/index.php/Color_output_in_console#less .
export LESS_TERMCAP_mb=$’\E[1;31m’ # begin bold
export LESS_TERMCAP_md=$’\E[1;36m’ # begin blink
export LESS_TERMCAP_me=$’\E[0m’ # reset bold/blink
export LESS_TERMCAP_so=$’\E[01;44;33m’ # begin reverse video
export LESS_TERMCAP_se=$’\E[0m’ # reset reverse video
export LESS_TERMCAP_us=$’\E[1;32m’ # begin underline
export LESS_TERMCAP_ue=$’\E[0m’ # reset underline
Your dashes before “quit”, “ignore”, “RAW”, “tabs”, etc. are not dashes. They are some other characters that look like dashes…
I copy/pasted your code to my bash profile, for some reason on here it’s not showing right, but is exactly as you put it in the article. I’m going to try again to copy/paste what I have in my profile below.
# set options for less
# https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
export LESS=’–quit-if-one-screen –ignore-case –status-column –RAW-CONTROL-CHARS –tabs=4 –no-init –window=-4′
# Set colors for less. Borrowed from https://wiki.archlinux.org/index.php/Color_output_in_console#less .
export LESS_TERMCAP_mb=$’\E[1;31m’ # begin bold
export LESS_TERMCAP_md=$’\E[1;36m’ # begin blink
export LESS_TERMCAP_me=$’\E[0m’ # reset bold/blink
export LESS_TERMCAP_so=$’\E[01;44;33m’ # begin reverse video
export LESS_TERMCAP_se=$’\E[0m’ # reset reverse video
export LESS_TERMCAP_us=$’\E[1;32m’ # begin underline
export LESS_TERMCAP_ue=$’\E[0m’ # reset underline
I just tested each options one at a time and –no-init is causing the no scroll, not sure why.
Thanks, I have added a note in the post. It turns out that
--no-init
also turns off mouse scrolling. See https://superuser.com/a/455213/139328I can no longer scroll. I can only scroll on sections that has been moved to using the arrow keys, spacebar or ‘G’. Please help.
Could you give a step-by-step procedure to reproduce the issue?
You missed the opportunity to name this “Make less Do More”.
🙂
(y)
Pingback: Debian. Настройка bash. | oleggelo