Most applications are smart about using the full width of the terminal available to them. My shell does, screen, vim, etc. However, diff in side-by-side mode (-y), does not. This angers me. It uses 126 character width, which is not even half of my 270 block width terminal.

Looking through the man page does not reveal anything particular about automatically adjusting the width, only manually specifying it via -W.

Is there perhaps a "hidden feature" of diff to allow it to automatically expand to the full width of the terminal? Or; What is the easiest path to automatically inserting -W <terminalsize> into my diff command whenever I run it?

I am under the assumption that automatically appending -W to everything couldn't hurt anything, because it will only ever mean "use $x columns", which is always what I want, whether I'm in side-by-side mode, or otherwise.


1 Answer 1

$ alias diff='diff -W $(( $(tput cols) - 2 ))'

ought to do it. You'll want to add it to ~/.bashrc as well.

The - 2 is mainly paranoia, in case something (embedded double-width Unicode?) expands enough to make the line wrap; if you want, you can just use

$ alias diff='diff -W $(tput cols)'
  • I started to notice $COLUMNS and was thinking something along the same lines. alias diff="diff -W ${COLUMNS}". I think I'd prefer to ask the shell rather than rely on the environment though.
    – VxJasonxV
    Commented Mar 15, 2011 at 22:17
  • 1
    tput considers $COLUMNS, stty -a, and the terminal description, using the first of those that has a meaningful value. eval $(resize) can be used to force them to be in sync with what the terminal thinks its size is.
    – geekosaur
    Commented Mar 15, 2011 at 22:22
  • note that if you have nonstandard tabstops (eg tabs 2 in your .bashrc) you will also need to use the -t flag or diff will produce very bizarre output
    – Jeff
    Commented Nov 10, 2015 at 1:54

You must log in to answer this question.

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