Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`git` and `less` integration in OS X terminal: prevent writing output of `less` to tty

In OS X terminal, when less is called on its own or by other programs like man, its output won't be written to the tty after we quit less. For instance, if we run less README, we would be temporarily directed to a screen with things like

SO rocks.
README (END)

And after pressing q, the output of less would disappear, and we end up with something like

$less README
$    # shell waiting for input

However, this is not the case if less is called by git (the pager of git is set to less -r in my case). The output of less is always written to the tty after quitting. For instance, if we run git log --oneline, if the log is short less won't even be called; if the log is longer than one screen, then we would be temporarily directed to the output screen of less as usual:

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

And after pressing q, the whole thing gets written to the tty, so we end up with something like

$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

So it is possible to change this behavior? I mean, is it possible to configure git so that it always pipes output to less (no matter the output is long or short), and leave nothing in the tty after less is quit? Thanks.

like image 453
4ae1e1 Avatar asked Nov 20 '13 02:11

4ae1e1


1 Answers

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

Yes. By default, git uses less as its pager, with the options FRSX [Apologies on the non-authoritarian source].

To get the behaviour you're after, you want to disable the F and X options. You can do this globally:

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

Read this answer for a longer explanation, and 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.

(I'm not sure why we want -X in this case, but apparently git uses it by default, and turning it off exhibits the behaviour you're after.)

like image 60
simont Avatar answered Nov 20 '22 21:11

simont