In
a
recent discussion on Reddit
I shared a number of tips
about the common utility
less(1)
that others found helpful
so I figured I'd aggregate some of those tips here.
While most folks invoke
less
at the tail of a pipeline like
command | less
less in a pipelineyou can directly provide one or more files to open
less README.txt file.c *.md
less directly
When reading a document,
sometimes you want to view another file,
adding it to the file list.
Perhaps while reading some C source code
you want to also look over the corresponding header-file.
You can add that header-file to the argument list with
:e file.h
You can navigate between multiple files using
:n
to go to the next file in the argument-list, and
:p
for the previous file.
You can also use
:x
to rewind to the first file in the argument-list
similar to how
:rewind
behaves in
vi/vim.
While I rarely feel the need to,
if you have finished with a file
and want to keep your argument list clean,
you can use
:d
to delete the current file from the argument-list.
Use
«count»G
to jump to a particular line-number.
So using
3141G
will jump to line 3141.
It helps to display
line numbers.
Similarly, using
«count»%
jumps to that percentage-offset of the file.
So if you want to go to ¾
of the way through the file, you can type
75%
to jump right there.
While many folks know you can search forward with
/«pattern»
and some people know you can use
?«pattern»
to search backwards,
or use
n/N
to search again for the next/previous match,
less
provides modifiers you can specify
before the pattern to modify its behavior:
!*@@*
Thus you would use
/@*«pattern»
to search for "pattern"
starting with the first file.
Using
&
lets you specify a pattern
and filter the displayed lines
to only those matching the pattern,
much like an internal
grep
command.
If you modify it with
!,
so it will display only those lines that do
not match
the pattern, like
&!«pattern».
I find this particularly helpful for browsing log-files.
You can bookmark points in a file with
m
followed by a letter,
then jump back to that bookmark with
'
followed by the same letter.
These apply globally across all open files,
so if you
ma
in the third file,
then navigate away to other files,
using
'a
will take you back to the marked location
in that third file.
I use marks most when reading man-pages,
dropping one mark at the
OPTIONS
section such as
mo,
and another at the
EXAMPLES
section,
such as
me,
then bounce back and forth between them with
'o
and
'e.
While you can use any of
the 26 lowercase or uppercase letters
(for a total of 52 marks),
I rarely use more than two or three
either in alphabetical order
("a", "b", "c"),
or assigning mnemonics like in the
man-page example above.
If the first line on the screen contains a
(,
[,
or
{,
typing that character
will jump to the matching/closing character,
putting it on the bottom line of the screen.
Similarly, if a closing
),
],
or
},
character appears on the last line,
typing that closing character
will jump to the matching/opening character,
putting it at the top of the screen.
I find it a little disorienting
if they fall less than a screen-height apart
because what feels like a forward motion
to find the next matching close-bracket
might actually result in shifting the screen
down
rather than
up
which feels backwards.
While I don't use it much,
you can also specify match-pairs using
alt+ctrl+f
or
alt+ctrl+b
followed by the opening/closing pair of characters
such as
alt+ctrl+f<>
to define a "<"…">" pair
and jump between them
in a manner similar to the
(/),
[/],
and
{/)
motions.
While the
man-page
documents many flags you can pass
on the command-line,
you can also toggle boolean options from inside
less.
I find this particularly helpful
when I've fed the output of a long-running process to
less
and don't want to re-run it
because it will take a long time.
Instead of quitting,
you can type a literal
-
followed by the option you want to change.
I most commonly want to toggle word-wrap for long lines,
so instead of quitting and adding
-S
at the end of my pipeline,
I can type
-S
directly in
less.
Options I commonly toggle:
-S-G-i/-I
-R-N/-n
The
!
lets you invoke an external command.
I don't do this often,
but occasionally I want some simple reference
like the current date
(!date)
or to do some simple math
(!bc).
$LESS
You might find yourself regularly setting
a common group of
options
so you can put those in your environment
(usually in your shell startup file like
~/.bashrc)
like
LESS="-RNe"
if you want to
show ANSI colors,
show line-numbers,
and exit automatically when you reach the end of the file.
less
has a few other corners that I've never really used,
but figured I'd document here:
While I've used tags in
vi/vim
to easily jump between definitions.
However, even though
less
provides support for tags generated by
ctags.
I've never found cause to use them.
The
v
command will open your
$VISUAL
editor on the current document.
less
lets you redirect the output
it has gathered from
stdin
to a file using the
o
command
(or the
O
command to overwrite an existing file).
This might come in handy because
less
won't let you
edit stdin
in an external editor
but you can write it directly to a file.