Less is More

Sometimes, after using a tool for many years, you will discover a feature that totally changes how you do things. When it is a fundamental tool like “less”, it changes how you interact with many command line programs in Linux*.

Did you know that less can do far more than just press space to advance to the next page? I sure didn’t. Then one day I went down the rabbit hole that is less and now I use it everywhere.

Let me demonstrate using our favorite database and favorite access tool - psql.

Let’s say that we are querying a customer's table. A simple “select first_name, last_name, email, city, country from customers" is going to get us started.


That is a lot of customers. We could easily add a filter to our query above, but what if we do not want to? Maybe the subspace link is too expensive, or we just want to easily view some aspect of the data without rerunning the query over and over.

If the data loaded into the pager (or if you selected “\pset pager always”), you are seeing this table through less.

Press “/”, followed by CTRL+k, then “serenity” to highlight all of our loyal customers with serenity emails.


That’s useful information, but if the data spans multiple screens, we’d have to scroll to find all the highlighted values. That’s not ideal if, for example, we want to copy and paste all of the records that use “Earth” for a country. Earth is not a country, so those values need to be fixed. We could copy highlight all of those sections that say “Earth” and then remove extra lines, but there is a better way.

Type “&” followed by “Earth” and press Enter.


Our screen changes to just show all of the lines that contain the word “Earth” in them. Much easier to copy all of them into an email. If you want to go back to the original list just filter again with empty pattern “&” + Enter. Sometimes you may need to scroll up to bring the original data back onto the screen.

That is really it. There are a few options for searching, color highlighting, but just these two little features changed how I interact with many programs. For example, did you know that “\h” and “\?” pages output through less as well? Try doing “\?” and then filtering for just the psql commands that accept a “FILE” as an attribute. Or maybe ones that operate on a buffer?

Less comes with its own help. Try pressing “h” on any less screen to see the available options. We can perform case insensitive pattern matching, jump to specific line of output, control scrolling, save output to a file and lots more.

There is even a way to keep your final, filtered output on the screen after you are finished. Simply export LESS=’-X’, and the output will remain available for further reference.

The best part is that less is used in so many places. Manpages, git log, systemctl status, journalctl are just a few common commands that use less in the background. Now you can highlight, search and filter any of that output.

Happy paging.

* Windows users are defaulted to using the “more”pager which does not have these capabilities. It is possible to install the GnuWin32 version of less. Once you do, make sure to edit your environment variables in ControlPanel. You want to add both the GnuWin32 and Postgresql bin directories to the PATH variable, as well as create the PAGER variable that points to the less program. At the time of writing this, the GnuWin32 less can perform searches and highlighting, but does not appear to have the filtering capabilities.