Less Tip

Published: Fri 14 August 2015
By EWS

In Blog.

This week has been a little rough, life has its ups and downs, and everybody partakes willingly or not in the "downs" every now-and-again. I'm not going to bore you with the details, suffice to say that it hasn't been "life as usual". As such, this weeks post is slightly "less"–as in: just a small tip on using Less.

The man page for Less states "less - the opposite of more". I'm not sure if that's useful at all or if I just haven't pondered that statement deeply enough, but it gives you enough to go on–Less is just a more functional version of More. To be honest, that description doesn't really do it justice; Less is way more functional than More, and happens to be my "go to" pager (is there another?).

Less allows one to page through text forwards as well as backwards, go to a particular line in the file (if indeed it's a file that you're Less-ing), go to the beginning, go to the end, search for a string using a regular expression plus a whole bunch of other things.

Today, though, I'd like to focus on a useful function of Less whereby one can filter everything that's being viewed. This function is particularly useful for viewing log files where you would like to limit the view to only those logs that you're interested in.

To filter lines down, just type & and then enter a regular expression, or for non-RE-proficiant mortals–just a string to filter to. From that point on, only those lines that match the regular expression will be shown.

Have you ever found yourself in the situation where you've entered a mode for a tool, but you don't know how to escape from it? I used to struggle with this for example when search highlighting happened to be enabled in Vim, and from the point of searching onwards, everything that matched the search item would be highlighted (in this case, the trick is to issue :noh, short for "No Highlight" to switch it off).

To switch the filtering feature off once enabled, just type & again, and then Enter.

As a useful extension to the filter feature of Less, there's the "filter-not" option, that is, "view everything but lines that match a regular expression". In this case, type &! and then the RE for matching lines that should not be shown. Once again, to switch this off, just type & and Enter.

Comments !

social