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
hOpen a summary of
lesscommands and options.qorZZquit
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.
fMove forward (down) a whole screen.
bMove backward (up) a whole screen.
dMove down half a screen.
uMove up half a screen.
gor<Go to the beginning of the output.
Gor>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
returnto jump to the next result.nGo to the next match for the search.
NGo 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.
-iToggle case sensitivity for searching, unless the search query contains capital letters.
-JPut a marker on the left edge of the screen on every line that the current search matches.
-WAfter a forward jump of more than one line, highlight the first new line. Unfortunately, this doesn't work for jumping backwards.
--status-lineWhen a line is marked (as with
-W), mark the whole line instead. Frustratingly, the version oflesson MacOS is too old to have this option; it was only added in 2021.--incsearchStart searching as soon as you start typing the search query.
--use-colorUse color in
less's output!-ROutput incoming escape characters in raw form. This helps when the input contains these control characters, like when running
git diffor 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.