top | item 31416815

(no title)

broses | 3 years ago

A while ago I was trying to find a way to make my terminal scroll back up after a command executed, so that if the output was long I could read it from the top without having to scroll up manually. There are ways to get yours shell to print something after the command executes, so I just needed to find an ansi escape sequence that would scroll up. Unfortunately I didn't see any sequences that do this. Anyone have any ideas?

discuss

order

bee_rider|3 years ago

Piping to less is essentially the standard way to do this.

your_command_here | less

It will capture the output and let you scroll up and down.

Otherwise, this would really be a feature of the terminal, and not necessarily required to be supported (it makes sense when you consider that many early real terminals literally printed the output to paper -- who needs scroll-back, just look up the tape! And repeating previously shown output would result in a confusing print-out).

However, if your terminal emulator doesn't have a scroll-back, you could try something like tmux, or alternatively gnu screen. These add lots of little features to the terminal (a buffer to scroll back, split terminals, persistence so you can detach and reattach) (tmux is more modern feeling, IMO).

duskwuff|3 years ago

As far as the "terminal virtual machine" is concerned, text which scrolls off the top of the screen is gone forever. Most software terminals implement functionality to save that content into a buffer and allow the user to review it, but there are rarely any control sequences which interact with scrollback, and behavior which interacts with scrollback (like resizing the window) often varies between terminals.

csdvrx|3 years ago

> Unfortunately I didn't see any sequences that do this. Anyone have any ideas?

Assuming you tried moving the cursor vertically, have you tried the alternative approach of Store Cursor Position / Restore Cursor Position cf https://stackoverflow.com/questions/28986776/ansi-escape-seq...

The scrollback buffer may be a limitation, so you should try with enabling or disabling the alternate screen (ti/te)

If that fails, you may have to tweak your terminal emulator to "hook" the SCP/RCP to a specific point of the scrollback buffer, to allow this scrollback.

midislack|3 years ago

Yeah it sounds like you need tmux and / or screen as the other anons have been saying. You can then split your terminal in its window, have a status bar which can display things, create multiple virtual terminals, view them on other terminals at the same time, etc. When I was first introduced to Unix in the mid-1990s I didn't know about screen and it would have saved me a lot of pain as I used a dial up connection. Once you start using one or the other there is no going back.

hnlmorg|3 years ago

Once it's scrolled off the top of the screen you're basically at the mercy of your terminal emulator's scrollback history. Some might have an escape sequence available to recall it but I there isn't any standard way of doing it.

You'd be better off piping into less / more / most. These are called "pagers" and are designed to do this. eg

    cat large-file | grep common-phrase | less

bowmessage|3 years ago

Maybe pipe output of every command into a script that invokes $PAGER when the output is long enough?

rustyminnow|3 years ago

less has a flag for that: (the gnu version does at least)

       -F or --quit-if-one-screen
       Causes  less  to automatically exit if the entire file can be displayed on the
       first screen.
So one could just pipe everything through `less -F`. If a different $PAGER is preferred, well that'll be a little more involved.

(Also handy: add in `-X` to leave the contents on the screen when you exit less)

SoftTalker|3 years ago

Run a tmux session, then you can scroll back as many lines as it preserves (configurable).

d0mine|3 years ago

vterm in emacs has a command to jump to the previous shell prompt.

mikevin|3 years ago

eshell has a module called eshell-smart that borrows from Plan 9's 9term, it automatically pages long commands, stays on the same line for editing if you show intent to change it and automatically scrolls to the end if you start typing a new command.