Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to keep the output of less on the screen after quitting?

Tags:

unix

oh-my-zsh

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?

like image 233
Luke Avatar asked Feb 03 '17 09:02

Luke


2 Answers

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
like image 174
Adaephon Avatar answered Nov 17 '22 17:11

Adaephon


For Git specifically, this can be handled with the following

git config --global color.ui true
git config --global core.pager 'less -Xr'
like image 17
Zach Posten Avatar answered Nov 17 '22 16:11

Zach Posten