21

I'm using oh-my-zsh which pipes the output of some functions like git diff and git log into less, whilst this is great for reading the output in the terminal. If I need to refer back to it it isn't possible after quitting with :q

Is there an option to preserve the current view on the file in my terminal after quitting?

Secondly, If there is an option where would I need to edit my oh-my-zsh config to ensure anything piped to less passes this option?

Luke
  • 3,481
  • 6
  • 39
  • 63

2 Answers2

26

To prevent less from clearing the screen on exit you can start it with the option -X:

less -X FILE

If you want to pass this option automatically to every instance of less, you can set the LESS environment variable accordingly in your ~/.zshrc:

export LESS="-X"

Note: If your shell has syntax coloring enabled, the -X option will cause your less output to display those color change escape sequences as inline ESC text.
This can be fixed by also passing the raw-control-chars display option, -r. For example:

export LESS="-Xr"

This also includes instances where less is started by another program, for example man. If you want to disable this option for a single command, you can just prepend LESS=. For example

LESS= man less
Daryn
  • 4,791
  • 4
  • 39
  • 52
Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • 4
    Adding this option on macOS makes my output contain the ANSI color escape sequences but not color itself. Like this: https://d3vv6lp55qjaqc.cloudfront.net/items/3f293X2l0s2p073q1V3y/Image%202017-08-20%20at%206.56.51%20PM.png. Any hints on how to fix it? – kolrie Aug 20 '17 at 21:59
  • 1
    Just for completeness, this did the trick for me: https://unix.stackexchange.com/a/62850/30787 – kolrie Aug 20 '17 at 22:03
  • 2
    `export LESS=-Xr` as described here https://unix.stackexchange.com/a/187559/303178 fixed that for me @kolrie – Bananaapple Jul 16 '19 at 07:29
19

For Git specifically, this can be handled with the following

git config --global color.ui true
git config --global core.pager 'less -Xr'
Zach Olivare
  • 3,805
  • 3
  • 32
  • 45