5
votes

so I want to alow horizontal scrolling in the terminal for my custom bash command by piping the output of the command to | less -S

However, if I do this, once you exit less, the output of the command will not stay in the command line history

Is there a way to configure less to keep the output of the command in the history when you exit less?

Eg. if you look at git diff, you can perform horizontal scrolling and then exit but the output still stays in the history in which you can then type new commands...I essentially want to emulate this for my custom bash command. Also in git diff it performs horizontal scrolling on the spot (ie. without using a new screen) and the command line history is still visible whereas for less it would do it in its own screen and you won't be able to see the command line history while "lessing". Would it be possible to emulate this git diff-like mechanism in less as well?

If there's any other solution without using less feel free to throw it out there

UPDATED QUESTION

So suppose I do this:

cat loremipsum | less -FRSX

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed 

And then I press right twice so that it scroll to the right twice. And then I press q to exit less. The output that remains on the screen will then be:

cat loremipsum | less -FRSX
    
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed 

 ESCOC

dictum diam. Nullam quis urna ullamcorper, accumsan quam vitae, aliquet 

 ESCOD

nisl. Maecenas vitae lorem orci. Ut vel est erat. Cras quis sagittis sapien, ac volutpat metus. 

 username@server:~/httpdocs$

However in git diff when you do the same and then press q, only the last part (ie the last part of the screen where I scrolled to the right)

 git diff

nisl. Maecenas vitae lorem orci. Ut vel est erat. Cras quis sagittis sapien, ac volutpat metus. 

 username@server:~/httpdocs$

Will remain on the screen

How do I get this to happen as well

3
"output" doesn't ever go into the command line history. Do you mean the in the terminal's dislay/scrollback buffer?Jim Garrison
@Kevin - does more support horizontal scrolling, which is being asked for here? If so, I can't figure out how to enable it.Digital Trauma
@pillarOfLight - Consider editing the question to clarify whether you really mean "command line history" or "scrollback buffer". hint - if you do, you'll get my upvote :)Digital Trauma
@DigitalTrauma I missed the horizontal part. No, I don't think it does.Kevin

3 Answers

11
votes

I don't have access to anything I can run git diff on right now, but if I read your question and comments correctly, I think the -X option to less would help. e.g.

ls -la | less -SX

This prevents less from saving and clearing the screen before displaying the output, and clearing and restoring the screen when it quits.

Update

I pulled a git workspace and figured out what git diff is actually doing:

$ pstree -p | grep git
        |-xterm(6021)---bash(6023)---sh(6104)---git(17708)---less(17709)
$ cat /proc/17709/cmdline | xargs -0 echo
less
$ cat /proc/17709/environ | xargs -0 --replace echo {} | grep LESS
LESSOPEN=|/usr/bin/lesspipe.sh %s
LESS=FRSX
$ 

So git diff passes no options to less. Instead it is setting the LESS environment variable to FRSX. This is the same as passing the -FRSX options to less. So, do you get what you need with -FRSX:

ls -la | less -FRSX
2
votes

It's not the command line history, it's the scrollback buffer of your terminal emulator.

Commands that use curses or ncurses typically use escape sequences that save and restore the terminal's state on startup on exit. On startup, a sequence switches to a secondary display buffer, where the command displays all its output. On exit, another sequence switches back to the primary buffer and restores the cursor position. This lets you edit or view a file and then still see your shell command history when you're done.

One workaround is to invoke less (or vim, or whatever) in another window, which you can leave running while still running commands in your shell. If you use xterm, for example, you can type something like:

command > command.log ; xterm -e less -S command.log

which is easily wrapped in a function or script. If you're not in a GUI environment, screen can do something very similar.

Another option, if you want the output of less to remain in your current window after you quit (which will scroll your shell command history off the top of the screen) is to use a terminfo definition that doesn't define those character sequences. The terminfo capability names are tmcup and rmcup. You may be able to configure your terminal emulator to disable these capabilities; see the titeInhibit resource for xterm, for example.

(There should be an easier way to do this, but I haven't found it.)

UPDATE :

As DigitalTrauma's answer points out, the -X (or --no-init) option to less inhibits the termcap/terminfo initialization and deinitialization strings (ti/te in termcap, smcup/rmcup in terminfo). Either adding -X to the less command-line options, or adding X to the $LESS environment variable, should do the trick.

Note that this applies only to less. Other full-screen commands, such as text editors, will still save and restore the screen by default. Using the titeInhibit resource for xterm, or hacking your termcap or terminfo entry to remove those escape sequences, will provide a more general solution.

0
votes

try this :

export LESS=X # it might be what you are looking for