On Unix how to get the output from the diff command to look like a man page? Where I can press ‘b’ or ‘f’ to read further or back

diff()man

You know how when you use the man command to read the manual (e.g. man ls) and you get output that is paginated that you can read from top to bottom by pressing 'f' to go read further down and 'b' to read further up (assuming you're reading from top to bottom left to right) and you press 'q' to quit. I want the output from diff to behave the same way because sometimes I have a long list of differences to read but I find myself having to scroll all the way up in the terminal to read the beginning of the output.

The Git diff command does this and I've come to like it (e.g. git diff ) but I don't always have the luxury of having all my files under version control.

The best I can think off is piping the out put of the diff command to some other command but I'm not sure which. Can anyone help? Does this sort of output behavior provided (like man pages) have a name for future reference?

Best Answer

It's called "paging output" or (somewhat erroneously) "pagination" …

… and man does it by invoking your preferred pager shell command, named by your PAGER environment variable, upon the output of whatever pipeline was used to generate the output form of the manual page. It falls back to a default if you haven't specified a pager command. On early Unices the default pager program, that man invokes as the default pager command with known hardwired options, was pg. On your system it is probably more or less. Some man commands look at other environment variables for pager commands, as well.

You do it by doing what man is doing: pipe the output of what you want to see into the standard input of a pager program.

You should get yourself a good book on Unix. There are a fair few that explain this, which is a very basic feature of the system. A simple Google Books search for pager unix more by me today turned up over a hundred books that discuss this. (I stopped counting at a hundred.)

Related Question