Assorted less(1) tips

74 points by fs111


vimpostor

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'
makishimu

Tags

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.
LeahNeukirchen

More: https://leahneukirchen.org/blog/archive/2015/02/six-hacks-for-less-1.html

telemachus

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.)

ilyagr

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):

  1. Do:
echo '^q toggle-option -redraw-screen\nq' >> ~/.config/lesskey
  1. Make sure 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)

iamnearlythere

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":

etc.

lalitm

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?

hayalci

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.

donio

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.