Assorted less(1) tips
74 points by fs111
74 points by fs111
I can also recommend reading through the less man page, as some flags are definitely worth enabling by default. For me I have long settled for:
export LESS='-Ric -x4 --use-color -Dd+r$Du+b'
-i: Use smart-case for searching (case-insensitive unless uppercase used)-c: It has always bothered me that without this, less starts at the bottom if not enough lines are printed. It breaks the UX invariant that text always starts at the top.--use-color: Enable colors for the less HUD, search, follow-mode, etc, much more delightful to use with this.-Dd+r$Du+b: If paired with MANROFFOPT='-c', this transforms the colors such that man pages are colorized just like with the popular colored-man-pages pluginThanks for this. I just added -c and -i to mine, but here's what I had previously. (I turn off colors globally as a preference.)
# Configure less:
# -G: no search highlighting
# -R: handle ANSI escape codes
# -J: show search locations in side column
# -x4: 4-space tabs
# P...: prompt containing [filename/STDIN] and [N%] for percentage through file.
export LESS='-GRJx4P?f[%f]:[STDIN].?pB - [%pB\%]:\.\.\..'
Good call! Mine is modest:
export LESS='ij4$Rg'
-g: Highlight only one particular string over ALL search matches-jN: Specifies a line on the screen where the "target" line is to be positionedTags
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.
mandoc(1) on OpenBSD does this. Since their man pages are marked up with elaborate semantic mdoc(7), they generate a convenient ctags file and pass it long via man(1) to less(1). It's such a blessing to be able to just jump directly to a flag or command definition vs. cycling through dozens of text match occurrences.
I've learnt about it from this post Using OpenBSD – for real:
And that's my current OpenBSD setup after a week of using it. I'm quite enjoying it, and still being pleasantly surprised by the quality-of-life from OpenBSD tools and documentation. For a small example, I can jump to sections or flag definitions in man(1) using :t. Systems without basic usability like that should be ashamed.
https://leahneukirchen.org/blog/archive/2015/02/six-hacks-for-less-1.html
Thanks for this. I had never bothered with lesskey, but cq is worth the price of admission alone! While looking at man less, I found out that lesskey is easier than ever to use.
Previous versions of less (before v582) used lesskey files with a binary format, produced by the lesskey program. It is no longer necessary to use the lesskey program.
man lesskey agrees:
This document describes the format of the lesskey source file, which is used by less version 582 and later. In previous versions of less, a separate program called lesskey was used to compile the lesskey source file into a format understood by less. This compilation step is no longer required and the lesskey program is therefore deprecated, although the file format remains supported by less itself.
These are great tips. I would summarize a lot of the highlights as "If you know vi/vim, you should know that less offers a lot of the same mechanisms." E.g., marks and {N}G to move to a line number (also G to jump to the end of the file and gg to return to the start).
One other trick worth mentioning is less +F instead of tail -f. See this post for discussion. And if you start with less file.log and want tail -f later, you can switch modes using the F command. (Use Ctrl-x to return to regular less viewing mode.)
There's a way to make ^q quit less and not clear the screen (like less -X), while having q quit less and clears the screen (like normal less):
echo '^q toggle-option -redraw-screen\nq' >> ~/.config/lesskey
less is invoked without -X (or with -+X if you want to be sure).This ^q command is particularly useful for git log output and other things where you might need to refer back to them in the next terminal command you do. (In fact, git uses less -XFR by default, so you'd need to override it to use less -FR instead for the above to work as intended). The q command is useful when you don't want to lose what you had on the screen before invoking less.
(I also cross-posed this at https://news.ycombinator.com/item?id=46472859)
I like the -p flag, which lets you search while invoking less, i.e. avoiding "less some_file, /search_term to search for something repetitive":
git -c log.pager='less -p^commit.*$' log -p and press n and p to navigate between commitsfind . -type f | xargs head -n99 2> /dev/null | less -iR --use-color -p '==> .* <==' --use-color --color=Skm to preview files and quickly jump between their filenames with n and p
etc.
This is fantastic. I've always wanted to learn the tricks of less but it's never bubbled as a priority. It's nice I can just refer to this now :)
One trouble I've always faced with less (and never put the time to understand) is for a process spewing a lot of output, I find it saturates the less buffer and then the process running gets into some "hung" state. These days I subconsciously press 'G' so all the output is buffered before I start to do any navigation. But is there some way to "unstick" the process in that state?
As there are bazillions of rewritten/reimagined command line tools, (fd, bat, hexyl, ...) I searched around to see if there's a replacement for less.
To my surprise only standalone alternative was ov (https://noborus.github.io/ov/)
bat has a --pager=builtin that invokes https://github.com/AMythicDev/minus but that doesn't feel like it belongs to the category.
For some extra Emacs-style line editing keys add these to your ~/.lesskey file. The comment line is significant, it marks the beginning of the section.
#line-edit
^p up
^n down
^f right
^b left
^a home
^e end
\eb word-left
\ef word-right
\ed word-delete
^w word-backspace
^k word-delete \ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed\ed
The wierd stuff at the end of the ^k line is a hack I got from somewhere long ago. It's necessary because less doesn't seem to have an emacs style delete to end-of-line command so instead we utilize the "extra-string" feature (see man lesskey) to inject a bunch of M-d commands to do hopefully enough word-delete's to simulate the desired C-k behavior.
Unfortunately some keys like C-e and C-k don't work at the beginning of the line because less captures them to set flags. Please let me know if you have a way around that.