Adventures With Less

by Ploum on 2022-01-07

In the Unix toolbox, less is probably one of the more common and useful items. Also one of the most overlooked. As I use it by default in Offpunk, I started to learn all the things you can and cannot do with it.

Default options for Offpunk

Current default in Offpunk is to launch "less -EXFRfM". What does that mean?

-E causes less to exit once it reaches the end-of-file. This means that Offpunk immediately falls back to its prompt once you reach the end of your content.

-F immediately quits less if the content could be displayed on the screen.

-X disables clearing the screen if not necessary. For very short contents (or very large screens), you still see previous contents above.

-EFX are very useful options which, together, make you feel like you are staying in Offpunk all the time. You can use Offpunk without knowing about "q" to quit!

-R is a very strange beast. In order to display colours in your terminal, your text needs to be escaped with ANSI escape sequences. By default, those are not displayed in less, which means no bold/colours/whatever. You can display them with "-r". But some of those escape sequences can mangle your text. Which is what was happening with some Gemini capsules in previous Offpunk and AV-98 release (the bug is basically that some lines disappear while scrolling but come back if you scroll back). Hopefully, the fix is to use -R which only displays escape sequences that will not cause screen problems, including colours.

-f forces less to open non-regular files. I’m not sure it is really useful but was used by Solderpunk so I kept it that way. It may help opening files with bad mimetype.

-M is about the little white line at the bottom called the prompt. Less has three prompts: short (by default), long (-m) and very long (-M). In Offpunk, the very long is useful as it displays the number of lines in the documents and your position. The only problem is that the temporary filename is also displayed, which is useless. Which brings me to the next topic.

Escaping %

Less is very customisable. With the option "-PM", I could customise the M prompt thanks to cryptic lines. I had a good experimental results with the following line:

less -EXFRfM -PM%s\ lines\ %lt-%lb/%L\ %Pb\%$ %s

Yes, it’s awful. It basically describes the line of the prompt. There are some very important problems. The first "%s" is supposed to be replaced, in Python, by the URL. I found it would be nice to have the URL displayed to know where you were. The very last "%s" is supposed to be replaced by the temporary file used.

Other "%" are part of the command line and should not be touched by Python. I didn’t find out how to do it. But, wait, there’s more. The "\%$" is not supposed to be interpreted because I want to display the "%" in the prompt (%Pb display the percentage of the position of the bottom of your current file. Without a "%", that number seems quite incomprehensible).

I’ve tried many escape methods, including raw Unicode name like \N{PER CENT} but Python immediately tries to match a "%" with a variable. And less doesn’t want me to put "%" in the prompt.

What less cannot do

Another feature I wanted to add was the ability to leave less and get back at the same position. That would allow leaving a long read, adding some links to your tour and going back to read. Something I do regularly.

Starting less at a given position is a no-brainer. Unfortunately, I didn’t find a way to get the position of the current file in less when quitting. I don’t know if it’s even possible. I find that this might be a really useful feature. As for the escape of "%", any help is appreciated. Post your answers to gemini@ploum.eu.

The more you use less

The more I’m used to less, the less I accept slightly different pagers. The most obvious one is the mutt pager. I’m constantly fighting with it. While, in theory, you can use less with mutt, this doesn’t really work (at least not directly like I tried). No colour, no info displayed. I’m thus forced to accept that most shortcuts from less will not work or not exactly the same way (once remapped). A real bummer.

I really start to dislike mutt because it doesn’t work without significant configuration, it’s full of arcane shortcuts and I’m never sure in what state I’m in. But, surprisingly, there’s very few mail client out there which support Maildir.

I’m even toying with the python maildir library to see if I could implement my own mail browsing tool with less and vim.

Conclusion

If you are reading it this far, you might be surprised how much can be said about less. And I believe I’m not even scratching the surface. We live in a world where, every two weeks, a huge new and exciting piece of software is released, doing the kitchen sink and all. We spend an awful lot of time learning that new stuff before switching to the next trend.

But if we took the time to really dig into what is already on our computer since 30 or 40 years, we would realise how complex it is in its simplicity. How powerful it is. How we could spend many lives just understanding their power. How we rarely would need anything else if we were able to really harness that power.

At least, that’s how I feel right now, after a week without constant connection, with only terminal windows opened and man pages as my sole documentation. If you allow me this awfully obvious pun, I can really say that, since disconnected, I’m doing more… with less !

As a writer and an engineer, I like to explore how technology impacts society. You can subscribe by email or by rss. I value privacy and never share your adress.

If you read French, you can support me by buying/sharing/reading my books and subscribing to my newsletter in French or RSS. I also develop Free Software.


Permalinks:
https://ploum.net/2022-01-07-less.html
gemini://ploum.net/2022-01-07-less.gmi