git - `git` 和 `less` 集成在 OS X 终端 : prevent writing output of `less` to tty

标签 git macos command-line terminal less-unix

在 OS X 终端中,less 被自己调用或被其他程序如 man 调用时,它的输出不会在之后写入 tty我们退出 less。例如,如果我们运行 less README,我们会暂时被引导到一个屏幕,上面有类似的东西

SO rocks.
README (END)

在按下 q 之后,less 的输出会消失,我们最终得到类似的东西

$less README
$    # shell waiting for input

但是,如果 git 调用 less 则不是这样(git 的 pager 是在我的例子中设置为 less -r)。 less 的输出总是在退出后写入 tty。例如,如果我们运行 git log --oneline,如果日志很短,less 甚至不会被调用;如果日志超过一个屏幕,那么我们将像往常一样暂时被定向到 less 的输出屏幕:

0000000 set the pager of git to less
......
1111111 what's wrong with git?
(END)

在按下 q 之后,所有内容都写入了 tty,所以我们最终得到类似的东西

$git log --oneline # OMG!!!
0000000 set the pager of git to less
...... (the entire log)
1111111 what's wrong with git?
$    # shell waiting for input

所以有可能改变这种行为?我的意思是,是否可以配置 git 以便它始终将输出通过管道传输到 less(无论输出是长是短),并且在 less< 之后不在 tty 中留下任何内容 退出了吗? 谢谢。

最佳答案

...is it possible to configure git so that it always pipes output to less...

是的。默认情况下,git 使用 less 作为它的寻呼机,with the options FRSX [对非专制来源表示歉意]。

要获得您想要的行为,您需要禁用 FX 选项。您可以在全局范围内执行此操作:

git config --global --replace-all core.pager 'less -+X -+F'

阅读this answer如需更长的解释,man less:

-F or --quit-if-one-screen
Causes less to automatically exit if the entire file can be displayed on the first screen.
-X or --no-init
Disables sending the termcap initialization and deinitialization strings to the terminal. This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.

(我不确定为什么我们要在这种情况下使用 -X,但显然 git 默认使用它,关闭它会表现出您正在之后。)

关于git - `git` 和 `less` 集成在 OS X 终端 : prevent writing output of `less` to tty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085990/

相关文章:

git - 根据提交哈希在 GitHub 上查找提交

eclipse - Eclipse 中项目验证时出错 - clean

macos - 我可以在我的 macOS 应用程序中嵌入第 3 方命令行实用程序并将其提交到 Mac App Store 吗?

c++ - 填充色和背景色的区别

command-line - 如何在 Visual Studio、C# 中以编程方式调用包管理器控制台命令,例如 NuGet 源上的 Get-Package?

git - 我可以在下次提交时跳过 git add 命令吗?

git - "This branch has conflicts that must be resolved"但它已经 merge

shell - 如何在所有php文件中递归地用\n替换\n\r

macos - 如何在终端中显示当前分支和文件夹路径?

regex - 使用sed将字符串逐行替换为正则表达式