Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent git from using pager for short output?

Tags:

git

When I was using Git Bash (Git For Windows) if a command (like git diff) had a short output (I'm guessing smaller than the terminal's height) it would just print as output, now in Babun (Cygwin) every Git command seems to be viewed in less even if it's one line or completely empty. "Every" is perhaps too bold, git status doesn't. It just seems like there was some pre-bundled setting in one of these that the other doesn't have and I don't know what it is.

How can I make Git behave so that when there is a short output it doesn't use less and instead just outputs it.

Edit: In git bash my pager for git is 'less -x4' (for 4 width tabs), no -F. Also, the environment variables LESS, PAGER, and GIT_PAGER are empty. So I have no idea why git bash is behaving like this, but luckily I've gotten some help on how to make Babun (Cygwin) start.

Hindsight update: (This is probably wrong, see 2017-01-12 note.) I think that Git For Windows and/or the default terminal doesn't wipe the screen when closing less -- I saw this behavior somewhere else that was unrelated (closing less and the screen not being wiped) so I think that's what is happening. Why Cygwin and the mintty terminal does the wipe and MINGW (or Msys2? Whatever Git For Windows uses) on the Windows terminal does not is beyond my realm of knowledge.

Hindsight update post Googling: Turns out the above revelation was enough info to Google the solution! I will post it now.

2017-01-12: Looks like it wasn't wiping it precisely because LESS was unset. According to man git config...

core.pager     [...]      When the LESS environment variable is unset, Git sets it to FRX     (if LESS environment variable is set, Git does not change it at     all). If you want to selectively override Git’s default setting     for LESS, you can set core.pager to e.g.  less -S. This will be     passed to the shell by Git, which will translate the final     command to LESS=FRX less -S. [...] 
like image 884
Captain Man Avatar asked Feb 05 '16 20:02

Captain Man


People also ask

How to turn off git pager?

To disable pagination for all commands, set core. pager or GIT_PAGER to cat .

What is git no pager?

--no-pager to Git will tell it to not use a pager. Passing the option -F to less will tell it to not page if the output fits in a single screen. Usage: git --no-pager diff.


2 Answers

As of December 2017 if you are using the latest version of less (at least 530) then the below is slightly incorrect. Now -F alone will properly output short output (as if by cat) while not needing -X. Since you can now use -F alone to see short output this means long output will still be properly wiped from the screen once you close less! I highly recommend you update your version of less to get this behavior. It's great!


Schwern's answer is half correct. For what I was asking it is probably still the correct answer, I was just using the incorrect words. What I wanted wasn't this:

   -F or --quit-if-one-screen           Causes less to automatically exit if the entire file can be dis‐           played on the first screen. 

That makes nothing appear for short output! If core.pager is less -F and your log is less than one screen, you see nothing.

What you probably want is less -FX or maybe less -X.

   -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  clear‐           ing the screen. 

This question over at superuser led me to this.

Using -FX if the log is less than one screen then it just outputs it (as if by cat).

Using only -X will still open less if the output is less than one screen but will leave it on the terminal if you quit, if you don't quit right away you can actually use less like normal. Once you try to search though it brings it into "real" less and still does not wipe when done, which is annoying because the windows is now full of ~'s.


Summary

  • If you don't care what happens with short output, use less
  • If you have an older version of less than 530 and you want short output to be put on the screen as if by cat use less -XF
    • If you have version 530 or newer then just use less -F. See the top of the answer for more info.
  • If you want to perhaps search short output (even though it's less than one screen) then use less -X, but you will have to still press q.
like image 178
Captain Man Avatar answered Sep 23 '22 19:09

Captain Man


Quick answer: Save the following line to your .bashrc

export LESS=eFRX 

The whys and wherefores are covered in the answers to How do I prevent git diff from using a pager? where the OP is actually looking to remove pagination completely.

like image 30
Robino Avatar answered Sep 20 '22 19:09

Robino