Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would it be possible to automatically page the output in zsh?

Tags:

zsh

Sometimes we run a command in the terminal, and the output is too large, and we forgot to put "| less" in the end. So I am wondering would it possible to page the output when it is too large in zsh?

I tried to implement this feature by using python and less:

#!/usr/bin/env python3
termHeight = 25
import sys
from subprocess import Popen, PIPE
p = Popen(['unbuffer'] + sys.argv[1:], stdin=PIPE, stdout=PIPE)
lines = []
for count in range(termHeight):
    line = p.stdout.readline()
    if not line:
        break
    print(line.decode('utf8'), end='')
    lines += [line]
if line:
    q = Popen(['less', '-Mr'], stdin=PIPE)
    q.stdin.writelines(lines)
    while True:
        line = p.stdout.readline()
        if not line:
            break
        q.stdin.write(line)
    q.communicate()

let's save this python script to p.py. So when we run "python p.py some commands" like "python p.py ls --help", if the output is more than 25 lines, this script will use less to display the output.

The problem is I can not get input from user. Which means this solution does not work with interactive program at all.

like image 845
ytj Avatar asked Dec 21 '22 08:12

ytj


1 Answers

Try adding this to your .zshrc

export LESS="-FX"
  • -F = “Causes less to automatically exit if the entire file can be displayed on the first screen.”
  • -X = “Disables sending the termcap initialization and deinitialization strings to the terminal.” (stops less clearing the screen)

For me this means less is used as the pager when there’s more than a screen of text, and zsh’s built-in pager (zsh -c '< /dev/fd/0', like cat) is used when not.

HTH

like image 194
Oli Studholme Avatar answered Jan 14 '23 13:01

Oli Studholme