Let me tell you something about „less“: You are probably underrating it for no reason. Of course you know „less“ is always there and it does it’s job – showing files while being able to scroll backward – and some even use it instead of „tail“. But, hey, let’s examine some of the command line options to get more out of less:
-M: this option extends the prompt on the bottom. By default less in most cases just shows the name of the file it is showing, with „-M“ turned on, it also shows how many lines the files has, which lines it is currently showing and how far (in percent) you have gone. No killer feature, but nice to have.
-i: this option causes searches to ignore cases. A search for „pattern“ therefore also finds „PaTTerN“. You like this, don’t you? You like this even more, as this search still enables you to switch case sensitive search on by searching for a pattern containing at least one uppercase letter. A search for „Pattern“ for instance would still be case sensitive. If you even want to prevent this, you could use „-I“ which totally ignores cases.
-r: Sometimes getting warnings about binary characters? With „-r“ you tell less to display raw characters. This can help you when displaying files containing color codes. It is said that log files from Rails contain these types of code.
-c: Just a gimmick to redraw the screen more clearly by beginning from the top line instead of scrolling. This might result in a slightly increased data transfer rate when using ssh but can improve usability.
-a: This causes less to skip found search patterns when pressing „n“ not from item to item but from page to page. You might know the pain when searching for a pattern that comes up more than once on a page and you start hammering „n“ getting confused on what you have already seen and what not. This options just skips at least the current page before displaying the next found pattern while still marking all patterns of course.
-f: This can help you in conjunction with „-r“ to force the display of raw characters without being questioned again.
Confused about the sequence of the options? Don’t be:
$ less -Mircaf
is something you just learn or create an alias for.
This one, to sum up the options, will display an extended prompt, ignores cases in searches, while being able to switch them on, skip found search patterns at least per page, display raw characters like color codes without asking and redraws the screen as good as possible.
Another feature that should be mentioned is the „follow file“ mode that some of you might know. It is similar to tail as it shows you the content of a file that gets appended while viewing. You turn this mode on by pressing „F“ (uppercase F). The advantage over tail is that you can interrupt the mode by pressing ctrl-c and scroll back though still being able to return to follow by pressing „F“.
Not so familiar is the fact that you also can jump into the follow mode from the command line:
$ less +F
starts the follow mode immediatly. Of course typing „+F“ on the command line is not sophisticated as typing „tail“ but you can create an alias for it like „ltail“ or whatever you like.
As a summary:
$ alias eless="less -Mircaf" $ alias ltail="less +F"
gives you two new commands. „eless“ as an extended less provides you with the described features. „ltail“ simulates „tail“ but enables you to jump back to the normal less by pressing ctrl-c.
Instead of creating an alias for „less -Mircaf“ you could also use the environment variable „LESS“:
$ export LESS="-Mircaf"
A credit goes to mnemonikk, who was just too lazy to blog this.
Please note: As less is still being developped, command line options might slightly change. For instance in newer version „-R“ instead of „-rf“ might lead to the same result. Just try it or check the version of less you are running („less –version“) against the official less changelog.
I also use less extensively and have found the .lessfilter file to be a gold mine.
Put the following in your ~/.lessfilter file:
#!/bin/sh
# Neat less trickery
# Joachim Nilsson
case „$1″ in
*.Z) uncompress –
if [ -s /tmp/less.$$ ]; then
echo /tmp/less.$$
else
rm -f /tmp/less.$$
fi
;;
*.[ch]) export LESS=“$LESS -R“; enscript -Ec –color -w ansi -o – $1
;;
esac
Now, run „less -r myfile.c“ and have syntax highlighted C, thanks to enscript!
I should blog about these things, I do it too rarely.
Have you tried out most ? It can do that bit more than less.
http://debaday.livejournal.com/24084.html
http://linux.die.net/man/1/most
It’s particularly good when used by man, for which you need to do one of
update-alternatives –set pager /usr/bin/most
OR
in your .bashrc:
export PAGER=/usr/bin/most
@Joachim: Thank you for the hint. Seems there are some details missing but I’ll figure out and probably report back.
@mish: Yes, I know most. The point is that less is installed by default on a lot of distributions. Therefore knowing it a bit deeper helps you on those systems where you either are not able to install software or just don’t want to. Nevertheless, thank you for the hint on using it as a man pager – I’ll give it a try and see how it differs.
Pingback: Internet Alchemy » links for 2008-06-03
I see it too, I just tried implementing my own tip on a new machine – failed miserably. Sorry about that.
The trick seems to be the citation („) and double dash’s (–) that get screwed up when I paste from Firefox into a gnome-terminal. In particular the –color (double dashes) got messed up.
That and the fact that not all installations have the following environment variables set. Ubuntu 8.04 and 7.10 seem to though.
setenv LESSOPEN ‚| /usr/bin/lesspipe %s‘
setenv LESSCLOSE ‚/usr/bin/lesspipe %s %s
I just made a quick search for it and found a better example:
http://www.speedblue.org/less_color.php
Regards
/Joachim