On Fri, Feb 10, 2017 at 08:26:11AM +0100, robin wrote:
> I usually pipe into less whenever something overflows the terminal
> height, but having to type 2>&1 to see stderr is a bit cumbersome. In
> dvtm Shift-PageUp is much easier.

I use a generic wrapper function in Bash:

    #   $1  Name or path of the command to execute.
    #   $2  White-space separated list of options to pass to the command
    #       when stdout is a TTY. If there are no TTY-dependent options
    #       this should be "--".
    #   $@  Arguments to pass to command.
    #
    function -paginate()
    {
        local errfd=1

        local command="$1"
        local tty_specific_args="$2"
        shift 2

        if [[ -t 1 ]]; then
            test "$tty_specific_args" != "--" || tty_specific_args=""
            test -t 2 || errfd=2
            "$command" $tty_specific_args "$@" 2>&"$errfd" | less -X -F -R
            return "${PIPESTATUS[0]/141/0}"  # Ignore SIGPIPE failures.
        fi

        "$command" "$@"
    }

Then I have around 30 aliases for various commands I use like this:

    alias cat='-paginate cat --'
    alias grep='-paginate grep --color=always'
    alias ps='-paginate ps --cols=$COLUMNS --sort=uid,pid -N --ppid 2 -p 2'

Output is only paginated when stdout is a TTY so I can still use pipes,
and the less flags ensure that less will exit if the output fits on one
screen. I also use tmux, but I find less to be less painful to use than
copy mode in tmux when I don't need to actually copy text.

Eric

Reply via email to