简体   繁体   中英

scrolling down git diff from mac terminal

I am using git on terminal on OS X 10 Yosemite.

When I do git diff I get long list of changes and sometimes I just need to scroll to the end but I couldn't figure out a way to do that other than to hold the keydown on MacBook pro and then it scrolls slowly.

if I press FN+down key then it does not scroll unless I have made the scroll down journey already down using the down button, but does not go further down...

any advice will be appreciate this is very annoying... :(

git diff uses the same pager as less Unix command.

  • Use keys d and u to go down / up half pages (forward / backward technically)
  • Jump to last line: G
  • Use h if you want to display the help

Another trick is storing the diff as a patch file like they used to do in the email days! Then you can open up the patch in any program (Sublime has syntax highlight red/green)

Some examples:

git diff master > ~/patch

git show someCommitSHA > ~/patch

git diff master myBranch -- *.js *.css > ~/patch patch of js & css diff from master


git apply ~/patch

Normally you would use the patch by applying the diff, but you can just open the file in any text editor. It's useful if you don't want to make a full commit out of your diff, but still want to use it somewhere else or send it to a friend


You can also use git diff master | grep -C 2 someKeyword git diff master | grep -C 2 someKeyword to show diff +/- 2 lines around some keyword

git config --global core.pager "less -+\$LESS -RS"

On my new macOS laptop, that got the git log scroll working as I'd expect (page up / page down buttons working, color, wheel scroll).

Why does this work? Per the git-config manpage :

Note that git sets the LESS environment variable to FRSX if it is unset when it runs the pager.

Per the less manpage :

-X 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 don't actually know what any of that means, but that X breaks page up / page down / scroll wheel.

-F Causes less to automatically exit if the entire file can be displayed on the first screen.

If you don't have X, then F breaks when there's no paging, presumably because less exits immediately and something prevents it from printing directly to the terminal.

Per mzabaluev's comment:

The OS X terminal has some magic integration with less which breaks when less is run as a subprocess by git: when run as git diff | less, scrolling forward is possible with the touchpad gesture, but with the output of git diff it only scrolls the terminal output and cannot tell less to scroll forward. Interestingly, git help does not suffer from this problem. mzabaluev Jan 4 at 0:39

This magic also includes enabling page up (FN+up) and pagedown (FN+down).

A quick and dirty way around this is to invoke less yourself with:

git diff --color=always | less -r

The command line options retain git's coloured output. I got them from Can less retain colored output?

You can use any of the less commands to navigate the history. See: frequently used less commands

Examples: In the bottom left of your terminal, you must be seeing something like this. 在此处输入图像描述

In the above example, the history has 221 lines out of which the current window is showing lines 21-62.

Go directly to a line number : You can directly go to a line by typing

line-number (followed by) g   (no spaces necessary)

for example: to go to line#100 just type 100g

Skip x-number of lines: Type a number and hit enter.

Scroll down and scroll down Use Fn

Or use below aternatives.

Hit space bar. as ILI suggested.

Scroll up by a page

Hit 'w'

Go home

Fn Left-Arrow Go to end Fn Right-Arrow

Easiest way is to use less to read the diff output. You need to use the --color and -R options to keep the coloration.

git diff --color | less -R

This works fine for me.

You could make a macro for this command.


Since I wrote this answer, I discovered that you can use the LESS environment variable to set options for less , so you can do the same as above even easier:

LESS=R git diff
brew install less
git config --global core.pager "less -+\$LESS -RSF"

Installing a newer version of less fixes the bug where -F causes small outputs to entirely disapear without -X , and then the config command removes -X , which is what breaks scrolling.

This will replace the less version MacOS ships with with the latest, but as they are the same utility (unlike GNU coreutils vs. BSD coreutils), this should not break things.

Explanation

This is an extension to ognockocaten's answer .

That answer suggests:

git config --global core.pager "less -+\$LESS -RS"

This removes two options:

-F / --quit-if-one-screen :

Causes less to automatically exit if the entire file can be displayed on the first screen.

and -X / --no-init :

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

This works for the question at hand, but is suboptimal, as less will always switch to an alternate page regardless of how small the output is. So, even for small commands like git diff , you'll get a full alternate screen.

Reading closely, it seems like re-adding -F would fix this. However, a combination of two things causes this to break:

  • on the version of less MacOS ships with, 487 , less will switch to the alternate screen first , before checking if the text is small enough, and thus print the output to the alternate screen buffer. This was fixed in version 530 (see https://unix.stackexchange.com/a/107355/39135 )
  • on MacOS, without -X , switching back from the alternate to main buffer clears the screen and thus erases all text that was just printed to the alternate screen buffer.

So just re-adding -F , you'd get small outputs being completely erased, as if they were never printed. We can't re-add -X to fix this, as that breaks scrolling, but we can update to a newer version of less !

Thus it all comes together - we can use homebrew to install the latest stable less version, and then configure the pager to include the -F option, as per the command block at the start of this post.

Just push h in terminal with opened less after git diff - it call help window with shortcuts

screenshot with less help text

Building up on @ognockocaten's answer, you can change the default options passed to the less command by changing the LESS environment variable.

To do that, add this line to your startup script. (probably ~/.bashrc , ~/.zshrc , or ~/.bash_profile )

export LESS="RS"

And it should work for any new terminal you open.

fn + option (⌥) + down

worked for me on mac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM