How to automatically pipe to less if the result is more than a page on my shell?

Asked
Active3 hr before
Viewed126 times

6 Answers

automatically
90%

In general, automatically piping to less requires the shell to be prescient about the output that will be produced by the commands it runs - and it is hard enough for humans to predict that without trying to make programs do so.,You could always pipe to less -E (this will cause less to automatically quit at the end of the file). For commands with short output it would do what you want. I don't think you can automatically pipe to less when there is a lot of output.,I wrote this wrapper function and put it in my .profile. You can use this before a command and it will automatically pipe it to less if it is longer than 1 page.,You could write a shell that does it for you - that captures the output (but what about stderr?) and paginates if necessary, but it would most certainly not be a standard shell.

Here is an example wrapper function which would need testing and perhaps some additional code to handle edge cases, etc.

#!/bin/bash

foo() {
   if [
      [-p / dev / stdout]
   ] # you don 't want to pipe to less if you'
   re piping to something
   else
      then
   command foo "$@" | less - F
   else
      command foo "$@"
   fi
}

If you use the same name as I have in the example, it could break things that expect different behavior. To override the function to run the underlying program directly precede it with command:

command foo
load more v
88%

I’ve got a solution that’s written for POSIX shell compliance, but I’ve tested it only in bash, so I don’t know for sure whether it’s portable.  And I don’t know zsh, so I have made no attempt to make it zsh-friendly.  You pipe your command into it; passing a command as argument(s) to another command is a bad design*., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. , @sourcejedi, not for me, not even with the latest 487 version. Do you have a $LESS variable with additional options? – Stéphane Chazelas Sep 14 '17 at 11:22 ,That should work as long as the output doesn't have other escape sequences or control/ill-encoded characters.

The secret ingredient: the fold command will break long lines the way the screen will, so the script can handle long lines correctly.

#!/bin/sh

buffer = $(mktemp)
rows = "$LINES"
cols = "$COLUMNS"
while true
do
   IFS = read - r some_data
e = $ ? # 1
if EOF, 0
if normal, successful read.
printf "%s"
"$some_data" >> "$buffer"
if ["$e" = 0]
then
printf "\n" >> "$buffer"
fi
if [$(fold - w "$cols"
   "$buffer" | wc - l) - lt "$rows"]
then
if ["$e" != 0]
then
cat "$buffer"
else
   continue
fi
else
if ["$e" != 0]
then
   "${PAGER:="
less "}" < "$buffer"
# The above is equivalent to
# cat "$buffer" | "${PAGER:="
less "}"
#… but that’ s a UUOC.
else
cat "$buffer" - | "${PAGER:="
less "}"
fi
fi
break
done
rm "$buffer"

Suppose you run some command with output processed by your display filter, and you look at the output, and decide that you want to save that output in a file.  If you had typed (as a hypothetical example)

ps ax | mypager

you can then type

!: 1 > myfile

or press and edit the line appropriately.  Now, if you had typed

mypager "ps ax"

Or suppose you decide that you want to run ps uax next.  If you had typed ps ax | mypager, you could do

!: 0 u!: *

echo {1..10000} is obviously just an example command; ps ax isn’t much better.  What if you want to do something just a little bit more realistic, like ps ax | grep oracle?  If you type

mypager ps ax | grep oracle

So you have to do what I was showing earlier:

mypager "ps ax | grep oracle"

What if the command line whose output you want to capture is even more complicated; e.g.,

command1 "arg1" | command2 'arg2'
$ 'arg3'
load more v
72%

HTML rendering created 2021-08-27 by Michael Kerrisk, author of The Linux Programming Interface, maintainer of the Linux man-pages project.

65%

Both cmd1 and cmd2 are command line utilities that output their results to the screen (stdout). When you pipe one command's output to another, however, the information from cmd1 doesn't produce output to the screen. The pipe redirects that output as input to cmd2. ,To remove this entry from your results, use the pipe operator again:,Was the output for either of these what you expected? By default, the sort command performs a dictionary sort on the first word or column, which is why Bob Jones and Terry Jones are not listed one after the other.,One of the most powerful shell operators is the pipe (|). The pipe takes output from one command and uses it as input for another. And, you're not limited to a single piped command—you can stack them as many times as you like, or until you run out of output or file descriptors.

I'm sorry to inform you, but the command line didn't die off with the dinosaurs, nor did it disappear with the dodo or the carrier pigeon. The Linux command line is alive and well, and still going strong. It is an efficient way of quickly gathering and processing information, creating new scripts, and configuring systems. 

One of the most powerful shell operators is the pipe (|). The pipe takes output from one command and uses it as input for another. And, you're not limited to a single piped command—you can stack them as many times as you like, or until you run out of output or file descriptors.

|
load more v
75%

I prefer it to ctl-r, as you see all the results at once (pip to less if you don’t want to pollute terminal buffer). With history numbers, then you can use:,I don’t know if this is what does the trick, but I can’t find any other customizations that I have made which would be relevant.,The problem with aliases? You go to a different machine which doesn’t have them and you don’t recall the original command.,Shells are great power tools. People who only know how to do things through a GUI, or know a little shell but not really enough to wield it properly, probably can’t understand what they are missing.

Your own personal(ized) terminal

There are lots of ways to customize your command line prompt and terminal to make you more efficient at work. We’ll start with possibly the most powerful one: meet ~/.bashrc and ~/.bash_profile

~/.bashrc

Your own personal(ized) terminal

There are lots of ways to customize your command line prompt and terminal to make you more efficient at work. We’ll start with possibly the most powerful one: meet ~/.bashrc and ~/.bash_profile

~/.bash_profile
load more v
40%

less is a terminal pager program on Unix, Windows, and Unix-like systems used to view (but not change) the contents of a text file one screen at a time. It is similar to more, but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, less does not need to read the entire file before starting, allowing for immediate viewing regardless of file size. ,By default, less displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat. ,Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.,Mark Nudelman initially wrote less during 1983–85, in the need of a version of more able to do backward scrolling of the displayed text. The name came from the joke of doing "backwards more." Originally, less was developed for Unix, but it has been ported to a number of other operating systems, including MS-DOS, Microsoft Windows, OS/2, and OS-9, as well as Unix-like systems such as Linux.[3] It is still maintained today by Nudelman.

The command-syntax is:

less[options][file_name]
less - M readme.txt # Read "readme.txt"
less + F /
   var / log / mail.log # Follow mode
for log
file * | less # Easier file analysis
less - I - g void * .c # Case insensitive search
for "void" in all.c files
load more v

Other "automatically-undefined" queries related to "How to automatically pipe to less if the result is more than a page on my shell?"