6

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?

4 Answers 4

11

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.)

10

The program auxiliary that man and git diff invoke is called a pager. On modern systems, the default pager is called less. Several decades ago, the first pager was more, so called because it displayed one page then waited for you to press a key to see “more”. Then came less, which also let you go back (to see less, so to speak), confirming the saying that “less is more” (than more).

git diff is a bit of an exception; the diff utility, the cvs diff subcommand, svn diff subcommand and so on just do their job of computing the diff and printing it out. If you want to page through the diff, you need to invoke a pager explicitly:

diff file.old file.new | less

If you want to use a different pager for man, git diff and other commands that call a pager, you can set the PAGER environment variable, for example by putting this in the file ~/.profile:

export PAGER=most

You can't make commands such as diff invoke a pager automatically. You can make a wrapper function though (to be put in your shell initialization file, e.g. ~/.zshrc for zsh or ~/.bashrc for bash):

diff () {
  if [ -t 1]; then             # If standard output is a terminal
    command "$@" | less        #    then pipe through less
  else                         # else
    command "$@"               #     run the command (and return its exit status)
  fi
}

Warning: such a function can be disruptive sometimes. You can also make a one-character alias for your pager, e.g.

alias p=less P=less

so you can type diff … |p or diff … |P when you want to page through the output of a command.

4

How about using the less command...EX:

 ls -l | less -CM
1

You can see the diff content in vim:

git diff | view -

Of course you can:

diff -u a.txt b.txt | view -

Enjoy!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .