Less for your git pager

Less for your git pager

posted in dev-setup on  • 

A git diff or git log starts the less program. Perhaps not the fanciest tool but more powerful than you might expect.

This post details the arguments that can be passed to less and also how to navigate once less is running. Things not relevant for git are omitted.

Your current configuration:

git config --get core.pager

If this returns nothing, there is room for improvement there!

TL&DR:

Configure something sensible:

# Short version
git config --global core.pager "less -eFiJM~ -j3 --tabs=3"

# Not so short version
git config --global core.pager "less --LONG-PROMPT --tabs=3 --quit-at-eof --quit-if-one-screen --tilde --jump-target=3 --ignore-case --status-column"
h : See all less commands.

Add a link to the man page and this topic is hereby extensively covered :)

Parameters for core.pager

Git defaults to less FRSX.

Display

Default cursor is just a :

-m , --long-prompt : Show # bytes passed
-M , --LONG-PROMPT : Show current lines displayed (ex: 1-57)


-N , --LINE-NUMBERS : Display line numbers
-s , --squeeze-blank-lines : Fold multiple empty lines to just one (doesn't fold empty + or - lines though)
-S , --chop-long-lines : Truncate long lines instead of wrapping
-x , --window=x : Set default window line size to x for PgUp/PgDown

Searching

-I , --IGNORE-CASE : Case insensitive search
-i , --ignore-case : Case insensitive search unless uppercase characters are found in the search pattern
-jX , --jump-target=X : with X a number: Show search result X lines from the top of the screen
-J , --status-column : Place * on lines with search results

Quitting

-e , --quit-at-eof : Exits less when reached end twice
-E , --QUIT-AT-EOF : Exits less when first reached end
-F , --quit-if-one-screen : Exits when output is less than 1 page
-~ , --tilde : Lines after end of file are blank, not ~

Moving around

Syntax used:

  • xp: Triggers for any number x followed by p.
  • xp: Also triggers when just pressing p.
  • Omitted: Many keys can be combined with Control for the same effect.

Going down

xe , xj , xENTER : Forward one line or x lines if x is provided
xd : Forward half a page. If x is specified it becomes the new default for d
xPgDwn , xSPACE , xf : Forward a page. x is optional, if provided it sets the size of the page (once)
xz : Sets the size of x as the page size (in lines) for all subsequent calls of f, SPACE and PgDwn
G , > : Forward to the end

Going up

xy , xk , CONTROL + P : Backward one line or x lines if x is provided
xu : Backward half a page. If x is specified it becomes the new default for u
xPgUp , xb , ESC + V : Backward a page. x is optional, if provided it sets the size of the page (once)
xw : Sets the size of x as the page size (in lines) for all subsequent calls of b, PgUp
g , < : Backward to the beginning

Searching

See searching parameters above for configuring case sensitivity.

/ : Search down for text or regex
? : Search up
n : Goto next match
N : Goto previous match
& : Search and show only the matching lines

Other

s + filename : Save to file and inspect with your weapon of choice
( , ) , { , } , [ , ] : When the character pressed is on top of the page, scroll to its counterpart
xp , x% : Go to x% position in document


Marking:

mx : With x any character. Mark this position as x.
'x : Goto the position marked as x

Quitting

ESC , SPACE : ESC followed by SPACE: continue scrolling after end of stream
q , Q , :q , :Q , ZZ : Exit less
CONTROL + l : Clear screen afterwards?

Summary

After a few years of just using PgUp and PgDown I guess this research was way overdue.


Other interesting reads
Tags: git