7 added 545 characters in body
source | link

UPDATE

tl;dr Solution: upgrade to less 530

From http://www.greenwoodsoftware.com/less/news.530.html:

Don't output terminal init sequence if using -F and file fits on one screen.

So with this fix we don't even need to bother determining whether to use -X on our own, less -F just takes care of it.

PS. Some other less configs that I use:

export PAGER='less -F -S -R -M -i'
export MANPAGER='less -R -M -i +Gg'
git config --global core.pager 'less -F -S -R -i'
#alias less='less -F -S -R -M -i'

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from https://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from https://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

UPDATE

tl;dr Solution: upgrade to less 530

From http://www.greenwoodsoftware.com/less/news.530.html:

Don't output terminal init sequence if using -F and file fits on one screen.

So with this fix we don't even need to bother determining whether to use -X on our own, less -F just takes care of it.

PS. Some other less configs that I use:

export PAGER='less -F -S -R -M -i'
export MANPAGER='less -R -M -i +Gg'
git config --global core.pager 'less -F -S -R -i'
#alias less='less -F -S -R -M -i'

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from https://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

6 replaced http://stackoverflow.com/ with https://stackoverflow.com/
source | link

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from http://stackoverflow.com/a/18000433/2487227https://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from http://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from https://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

5 fix tab handling
source | link

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# BSD/OSX compatibility
[[ $(type -p gsed) ]] && SED=$(type -p gsed) || SED=$(type -p sed)
CONTEXT=$(catexpand <&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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from http://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# BSD/OSX compatibility
[[ $(type -p gsed) ]] && SED=$(type -p gsed) || SED=$(type -p sed)
CONTEXT=$(cat <&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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from http://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

I eventually ended up with writing a wrapper on my own.

#!/usr/local/bin/bash

# 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"

BSD/OSX users should manually install gnu-sed. The amazing regexp, which helps remove color codes, is from http://stackoverflow.com/a/18000433/2487227

I've saved this script to /usr/local/bin/pager and then git config --global core.pager /usr/local/bin/pager

The treatment for OCD patients, hooray!

4 `head` may cause issues when trailing newlines...
source | link
3 fix
source | link
2 optimization: no need to convert the whole input
source | link
1
source | link