Assumed audience: People interested in learning how the command lines tools on their computer work.
The summary provided by man less
is “opposite of more”, and the description opens with “…a program similar to more(1)
, but which allows backward movement in the file…” Not that helpful — especially since more
is aliased to less
on a lot of modern OSes, including macOS.
A better summary: less
displays a file in your terminal, page by page — thus, a “pager” — with navigation commands stolen from more
and vi
… so good luck quitting! The way out is to hit q
, but the man page does not tell you that, perhaps assuming you know more
and vi
.
As with many Unix commands, the “just do one thing” philosophy is… not really intact. It accepts most of the upper- and lowercase English alphabet as flags, in most cases modifiable with +
, for something around 80 individual ways you can invoke it. Many of them combinable!
Some things you can do with it:
-
Move forward and backward in a file, as promised by the description.
Space
,^V
,f
andF
all go forward by a screenful.b
and^B
andESC-v
(orMeta-v
) go backward by a “screen”. But “screen” is in quotes for a reason: -
You can change how big the “screen” is. This does not change anything about the amount displayed on screen (that’s up to your terminal), but rather how big a “screen”-sized is. You can set it to (e.g.) 8 lines explicitly with
-z8
or by precedingz
with a number of lines. -
Which applies more generally: you can stick
N
in front of lots of commands and it will affect the resulting “size” of the operation. Want to go down by just 8, and make that the default?8d
will do it.12u
will go up in the file by 12 lines and set the default.There are also variants of these which let you scroll past the start or edge of the page… as will the totally-new-to-me
→
and←
(orEsc-(
andEsc-)
) commands to scroll right or left. I guess that makes sense, but since things usually get auto-wrapped, I never knew! -
Jump to matching brackets: type the opposite bracket to get to the matching bracket, so
{
jumps to a matching}
if the top line in the screen has a{
in it; the same thing works for () and []. -
Search: you type
/
and then provide regex-ish syntax. Once you’re in that mode,n
goes to the next match andp
to the previous. Interestingly,less
remembers that across sessions! (Other tools might as well, butless
is the main one where I notice that behavior.)If you want to search backward, you can use
?
instead of/
. (Mnemonic:?
isShift-/
on a US English keyboard.) You can also modify the search query with flags before the search term, e.g. to find lines which don’t match (!
or^N
), wrap at the end (^W
), etc. -
You can work with multiple files! I had no idea. There are commands to skip to/search across/etc. multiple files —
:n
for next file and:p
for the previous file — even just straight-up open a new file from within the current session (:e
).
I haven’t even mentioned the CLI flags! There’s a ton here, but the one I’m keeping is that +
resets options to their default!
There’s a bunch more, but that’s a good bit of coverage for today. I don’t use less
much these days (I mostly use bat for a pager)… except when reading man pages, ironically!1 But it’s good to know a bit more about how it works. 🤓
Notes
You can set
PAGER
to usebat
as the default pager, andMANPAGER
to set the pager for man pages, so I could switch this (see the docs for usingbat
that way here); but I have not generally found it worth changing the default there. What I really miss is being able to launch a man page as a PDF easily, which macOS lost the ability to do for reasonable security reasons in few versions ago (the renderer was old and not well-maintained; I would rather they have taken the time to maintain it instead, but here we are). ↩︎