Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On my Debian machine here, less always opens the alternate screen to show stuff.

This is annoying, if there are only 2 or 3 lines to be displayed. I'd like less to:

  • work as pager, if there is more than a screenful of information

  • and work like cat, if there's not.

Apparently, less has the -F flag for "single screen" cases:

-F or --quit-if-one-screen

Causes less to automatically exit if the entire file can be displayed on the first screen.

But in my case it just exits again, and no info is displayed. It's more like cat /dev/null and thus not really useful.

Has anyone an idea, how to achieve this less behaviour?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can combine -F with -X, which disables the terminfo initialization sequence.

export LESS=-FX

This has the (dis)advantage that less does not clear displayed text on exit, no matter how long the file was.

share|improve this answer
    
Cool, that is quite exactly what I was looking for. That the screen is not cleared is just a minor drawback. –  Boldewyn Nov 26 '10 at 19:52

You could create a small wrapper script like so:

#!/bin/bash
if (( `wc -l < $1` < 20 ))
then
        cat $1
else
        less $1
fi

If you create that in /usr/local/bin as a file called something like less2 (you might want to use a very short name like l for easy typing) and make sure it is executable with chmod a+x /usr/local/bin/less2 you can use it in place of less for commands of the form less <filename>, i.e. less2 <filename>.

This won't work if you are directing another commands output through less.

It will no doubt be possible to do the same thing without the extra script file, by defining an alias, if you prefer.

share|improve this answer
1  
s/an alias/a function/ Attempting to cram an if statement that uses $1 into an alias is road to madness. Defining a function, on the other hand, is as easy as less() { if ...; then cat "$1"; else command less "$1"; fi; } –  grawity Nov 26 '10 at 13:38
    
You could update the script with use of HEIGHT=$(stty size | awk '{ print $1 }'). Then, yes, would it be a useful extension. Thanks for this option. –  Boldewyn Nov 26 '10 at 19:56
    
@Boldewyn: bash has $LINES (and $COLUMNS), which it updates automatically upon receiving a SIGWINCH. (And, if checkwinsize is on, every time the prompt is displayed.) –  grawity Nov 26 '10 at 20:21
    
I use this code github.com/mpapis/home_dotfiles/blob/master/.functions#L3-30 it will cat short files and will enable line numbers when file names ends with :{line} –  mpapis Aug 29 '12 at 3:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.