Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paginate git output only when necessary

Tags:

git

Is there a way to configure git to pipe its output into a paper only when it exceeds the terminal size (or at least a specific number of lines)? I think it's rather annoying that e.g. a 6-line diff is shown in the pager - hiding any previous output, and requiring me to explicitly press "q" to exit.

like image 448
Nikratio Avatar asked Jan 14 '16 17:01

Nikratio


2 Answers

On most systems, less is the default pager that Git uses and you can configure less to behave like you describe with:

git config --global core.pager "less -X -F"

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

See this answer for more options with configuring less.

Also, specifically with regards to the diff command you can use:

git --no-pager diff
like image 161
Jonathan.Brink Avatar answered Oct 17 '22 08:10

Jonathan.Brink


You can configure your pager by setting the PAGER variable, and any program which needs a pager will use it. less has an option -F which does exactly this. Adding export PAGER="less -F" to your ~/.bashrc will make this permanent.

Personally I use less -FXRS; see the man page for less for more details.

like image 33
db48x Avatar answered Oct 17 '22 10:10

db48x