A better pager for Git

It's only fair to share...Tweet about this on TwitterShare on Google+Share on FacebookShare on TumblrEmail this to someone

Git automatically calls less if the output is fairly long in the terminal. It is rather useful to avoid terminal history being violated and allows user to search through the output.

However, plain less isn’t the perfect solution. What I (and I believe many others also) want is:

  • Always colored output
  • Scrolling by touchpad or mouse
  • Auto-quit-if-one-screen

less -+F -+X -+S does everything except the last one. But if I remove -+F, there will be no output in case of one-screen; if I remove -+X as well, the output is back but it disables scrolling. I opened a thread in StackExchange but no satisfying answers came up.

So here’s a Bash script I wrote:


# BSD/OSX compatibility
[[ $(type -p gsed) ]] && SED=$(type -p gsed) || SED=$(type -p sed)
CONTEXT=$(expand <&0)
[[ ${#CONTEXT} -eq 0 ]] && exit 0
CONTEXT_NONCOLOR=$( $SED -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" <<< "$CONTEXT")
LINE_COUNT=$( (fold -w $(tput cols) | wc -l) <<< "$CONTEXT_NONCOLOR" )

[[ $LINE_COUNT -ge $(tput lines) ]] && less -+X -+S -R "$@" <<< "$CONTEXT" || echo "$CONTEXT"

It works fine with Bash. But when things come to Zsh/Fish the case could be more complicated as the prompt itself may take multiple lines. I’m still looking for a proper solution for this.


メールアドレスが公開されることはありません。 * が付いている欄は必須項目です