June 2024
I noticed recently that I was having trouble moving around in less
. I could arrow up and down, but that took a lot of work. I could hit space
to go a whole screen down, but unlike in a web browser, shift-space
also scrolls down. How could I scroll up a screen? On some terminals, Page Up
and Page Down
work, but not in others.
It's a confusing mess.
So, after years and years of using less
, I finally looked up how to move around natively. Native less
commands will work everywhere! Here's the most useful things I found. As a bonus, I also ran into some ways to configure less
to be more helpful!
General less
commands
h
Open a summary of
less
commands and options.q
orZZ
quit
less
. This isn't exactly navigation, but is pretty important.
Navigation
These commands move around in the output.
- up, down
Move a single line up or down.
- left, right
Move a half screen left or right.
f
Move forward (down) a whole screen.
b
Move backward (up) a whole screen.
d
Move down half a screen.
u
Move up half a screen.
g
or<
Go to the beginning of the output.
G
or>
Go to the end of the output.
They also work with the control
modifier, if for some reason you want to press more keys.
I've been finding that d
and u
are pretty easy to remember, but a lot of the other ones are more difficult. Some of the commands are taken from vi
, so if you already know vi
, that helps.
Searching
When I'm using less
, there's generally a lot of text. Fortunately, less
lets you search in the text it's displaying!
/
Begin a search. Type your query, then press
return
to jump to the next result.n
Go to the next match for the search.
N
Go to the previous match for the search.
Options
Less is actually configurable! Here are some command line options. If you prefer – perhaps because less
has been spawned from another program – they can also be typed once less
is running.
-i
Toggle case sensitivity for searching, unless the search query contains capital letters.
-J
Put a marker on the left edge of the screen on every line that the current search matches.
-W
After a forward jump of more than one line, highlight the first new line. Unfortunately, this doesn't work for jumping backwards.
--status-line
When a line is marked (as with
-W
), mark the whole line instead. Frustratingly, the version ofless
on MacOS is too old to have this option; it was only added in 2021.--incsearch
Start searching as soon as you start typing the search query.
--use-color
Use color in
less
's output!-R
Output incoming escape characters in raw form. This helps when the input contains these control characters, like when running
git diff
or a similar command.
Enabling options permanently
The settings can be set in an environmental variable LESS
.
For example, if you're using bash, you might add this to your ~/.profile
:
export LESS="-J -i -W --status-line --incsearch --use-color -R"
Or, for Fish shell, run this command:
set -Ux LESS "-J -i -W --status-line --incsearch --use-color -R"
I set them in my shell, and now everywhere I use less
, it works the way I want it to work. More or less.