在没有清除命令行历史记录输出的情况下,将管道减少到-S

时间:2022-06-08 14:04:12

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

所以我想通过将命令的输出传递给|来在终端中为我的自定义bash命令提供水平滚动少-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?

当你退出较少时,有没有办法配置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?

例如。如果你看看git diff,你可以执行水平滚动然后退出但输出仍然保留在历史记录中,然后你可以输入新的命令...我本质上想要为我的自定义bash命令模拟这个。同样在git diff中,它在现场执行水平滚动(即不使用新的屏幕),命令行历史记录仍然可见,而为了更少,它将在自己的屏幕中执行,您将无法看到命令线路历史,而“减少”。是否可以在更少的情况下模拟这种git diff-like机制?

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:

然后我向右按两次,使其向右滚动两次。然后我按q退出更少。屏幕上保留的输出将是:

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中你做同样的事情,然后按q,只有最后一部分(即我滚动到右边的屏幕的最后一部分)

 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 个解决方案

#1


10  

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.

我现在无法访问任何可以运行git diff的内容,但是如果我正确地阅读了你的问题和评论,我认为-X选项可以帮助减少。例如

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:

我拉了一个git工作区,弄清楚git diff实际上在做什么:

$ 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:

所以git diff没有将选项传递给less。相反,它将LESS环境变量设置为FRSX。这与将-FRSX选项传递给less相同。那么,你得到了-FRSX你需要的东西:

ls -la | less -FRSX

#2


2  

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.

使用curses或ncurses的命令通常使用转义序列,在退出时启动时保存并恢复终端的状态。启动时,序列切换到辅助显示缓冲区,其中命令显示其所有输出。退出时,另一个序列切换回主缓冲区并恢复光标位置。这使您可以编辑或查看文件,然后在完成后仍然可以看到shell命令历史记录。

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:

一种解决方法是在另一个窗口中调用less(或vim,或其他),您可以在shell中运行命令时保持运行。例如,如果您使用xterm,则可以键入以下内容:

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.

它很容易包含在函数或脚本中。如果您不在GUI环境中,屏幕可以执行非常类似的操作。

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.

另一个选项,如果您希望在退出后将less的输出保留在当前窗口中(这将在屏幕顶部滚动shell命令历史记录)是使用不定义这些字符序列的terminfo定义。 terminfo功能名称是tmcup和rmcup。您可以配置终端仿真器以禁用这些功能;例如,请参阅xterm的titeInhibit资源。

(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.

正如DigitalTrauma的回答所指出的那样,-X(或--no-init)选项可以更少地禁止termcap / terminfo初始化和取消初始化字符串(在termcap中为ti / te,在terminfo中为smcup / rmcup)。将-X添加到较少的命令行选项,或者将X添加到$ LESS环境变量,应该可以解决问题。

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.

请注意,这仅适用于较少。其他全屏命令(如文本编辑器)仍默认保存和恢复屏幕。使用titeInhibit资源进行xterm,或者使用termcap或terminfo条目来删除这些转义序列,将提供更通用的解决方案。

#3


0  

try this :

试试这个 :

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

#1


10  

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.

我现在无法访问任何可以运行git diff的内容,但是如果我正确地阅读了你的问题和评论,我认为-X选项可以帮助减少。例如

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:

我拉了一个git工作区,弄清楚git diff实际上在做什么:

$ 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:

所以git diff没有将选项传递给less。相反,它将LESS环境变量设置为FRSX。这与将-FRSX选项传递给less相同。那么,你得到了-FRSX你需要的东西:

ls -la | less -FRSX

#2


2  

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.

使用curses或ncurses的命令通常使用转义序列,在退出时启动时保存并恢复终端的状态。启动时,序列切换到辅助显示缓冲区,其中命令显示其所有输出。退出时,另一个序列切换回主缓冲区并恢复光标位置。这使您可以编辑或查看文件,然后在完成后仍然可以看到shell命令历史记录。

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:

一种解决方法是在另一个窗口中调用less(或vim,或其他),您可以在shell中运行命令时保持运行。例如,如果您使用xterm,则可以键入以下内容:

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.

它很容易包含在函数或脚本中。如果您不在GUI环境中,屏幕可以执行非常类似的操作。

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.

另一个选项,如果您希望在退出后将less的输出保留在当前窗口中(这将在屏幕顶部滚动shell命令历史记录)是使用不定义这些字符序列的terminfo定义。 terminfo功能名称是tmcup和rmcup。您可以配置终端仿真器以禁用这些功能;例如,请参阅xterm的titeInhibit资源。

(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.

正如DigitalTrauma的回答所指出的那样,-X(或--no-init)选项可以更少地禁止termcap / terminfo初始化和取消初始化字符串(在termcap中为ti / te,在terminfo中为smcup / rmcup)。将-X添加到较少的命令行选项,或者将X添加到$ LESS环境变量,应该可以解决问题。

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.

请注意,这仅适用于较少。其他全屏命令(如文本编辑器)仍默认保存和恢复屏幕。使用titeInhibit资源进行xterm,或者使用termcap或terminfo条目来删除这些转义序列,将提供更通用的解决方案。

#3


0  

try this :

试试这个 :

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