Recently I got around to configuring less
, and I collected these few tricks:
Sometimes I look at lists with
less
, and then do things step-by-step, keeping the current action at the top of the page. This works nicely until you end up at the last page of the file, and then can’t scroll down. You lose track of where you are at and get confused.It would be much nicer scrolling down, and filling up the buffer with
~
after the end of file, just as if you had searched in the pager.Actually, with
ESC-SPC
, you can move a full page down, filling up the buffer with~
. Toying around a bit, you’ll find out that you can override the “page length” with a prefix, i.e.1 ESC-SPC
will move down one line only!However, this is still inconvenient to type all the time, thus let’s define a keybinding. For this, create a file
~/.lesskey
where we will put the key definitions. This file then will be compiled usinglesskey(1)
and generate a binary configuration file~/.less
. (I guess you can be lucky thatm4
is not involved in this mess…)One problem is actually binding the key. You can easily bind the cursor down key (
\kd
) toforw-screen-force
, but how do you pass 1? The canonical hack is to use thenoaction
action, which will behave just like you’ve typed the keys after it. Thus, we write:#command \kd noaction 1\e\40 j noaction 1\e\40
(By the way, that
#command
comment is important to telllesskey
you are defining key commands.)Finally, scrolling bliss!
Actually, scrap that.
The badly underdocumented key
J
(andK
) will scroll how I want, but you only read about that in the example insidelesskey(1)
. Therefore, we can just do:#command \kd forw-line-force j forw-line-force
These keybindings are there since at least 1997 and I’ve never found them before…
While we are redefining keys, I’ve always found it a bit clumsy to read multiple files, having to type
:n
and:p
. Using[
and]
is much more convenient (at least on a US keyboard), and by default these keys do things of questionable utility.#command [ prev-file ] next-file
Did you ever wish to give feedback from
less
? Like have a script output some info, and you decide how to go on? Sinceless
always exits with status 0 usually, this I thought this was tricky to do, but thequit
action actually can return an arbitrary exit code, encoded as a character.I bound
Q
and:cq
(like in vim) to exit with status 1:#command Q quit \1 :cq quit \1
Now you can do stuff like look at all files and have them deleted when you press
Q
instead ofq
to exit:for f in *; do less $f || rm $f; done
I use
less
a lot to look at patches,git log
output, and ocassionally mailboxes. TheD
command as defined below will move to the next line starting withdiff
orcommit
orFromā£
.#command D noaction j/\^diff|commit|From \n\eu
It will also “type”
ESC-u
to hide the highlighting. Now I can simply pressD
to jump to the next chunk of interest.To return to where you started from after a search or going to the end of file, type
''
. Typing''
again will go back, so this is also nice to toggle between two search results.Back in the old days of X11R2(?) there was a tool called
xless
, which was exactly that: a pager likeless
that ran in its own X11 window. It’s quite useful. We can recreate this by combining a X11 terminal emulator and plainless
with a smallzsh
snippet:xless() { { exec {stdin}<&0 {stderr}>&2 exec urxvt -e sh -c "less ${(j: :)${(qq)@}} </dev/fd/$stdin 2>/dev/fd/$stderr" } &! }
Watch the trick how we pass the stdin/stderr file descriptors and the file arguments!
Now you can just run
command-spitting-out-loads | xless
and the output will be shown in a new terminal and not lock your shell.NP: Feine Sahne Fischfilet—Dreieinhalb Meter Lichtgestalt