11

I am looking for something I can install on an Ubuntu server which will allow me to compare the content of PHP, Javascript, CSS, HTML and other plain text files in a more presentable and understandable way than the default diff command.

Must have

  • Side-by-side comparison of file contents indicating lines that are different from file 1 to file 2
  • Run from the console (no desktop environment installed)
  • Support plain text files

Would like to have

  • Colours used to highlight changes
  • Synchronised scrolling (lines of both files stay side-by-side and in view as you move through the comparison)
  • Indication of changes within a line (eg. comparing the lines "a question" to "this is a question" would indicate/highlight that "this is" is the difference, as opposed to just indicating the whole line as having something different in it)

Optimally I would love something like WinMerge's file comparison feature: WinMerge

for Ubuntu but I understand that not all features may be possible for a console-only app.

1

3 Answers 3

11

vimdiff

vimdiff file1 file2

You can also use it with git, for example:

git difftool --tool=vimdiff HEAD^ HEAD

vimdiff

6

So you want an interactive diff tool with very basic features. The obvious answer is Emacs (Ubuntu: emacs), which includes Ediff since about 20 years ago.

Open the two files that you want to compare, and select “Tools → Compare (Ediff) → Two Buffers…” in the menu. Or select “Tools → Compare (Ediff) → Two Files…” and enter the file names. You can use the command ediff-buffers or ediff (alias of ediff-files) instead (press Alt+X then type ediff and press Enter). The main commands in Ediff mode are:

  • | to switch between showing the two files one above the other (default) or side by side
  • n to move to the next different block of lines
  • p to move to the previous different block of lines
  • q to quit Ediff mode
  • ? for help
  • C-x o (Ctrl+X then lowercase O) to navigate between the three buffers (file 1, file 2 and the Ediff control panel).

Ediff can also do 3-way diffs if you have a common ancestor file, and can assist in merges. The entries in the “Tools” menu should be enough to get you going; see the manual for details.

Emacs can also automatically pull revisions from svn and other revision control systems. Look under the “Tools → Version Control” menu or in the manual for more information.

You may be more comfortable using the GUI version of Emacs. Emacs works on all major operating systems (on Windows, I use EmacsW32) and can access remote files transparently through SSH (and other methods). To open a remote file via SSH, type /name-of-host:/path/to/file as the file name. Under Windows, if you're using PuTTY for SSH, make sure to have plink installed and on Emacs's PATH, and put (setq tramp-default-method "plink") in your ~/.emacs. See the Tramp manual for more details.

1

If you like diff but want it a bit more easy to understand and read, there's colordiff - basically the same as diff but with coloured highlighting.

To get a side-by-side comparison using diff or colordiff you can add the -y option. You can also use sdiff - it outputs a side-by-side comparison a bit cleaner than diff with the -y option (on Fedora at least, on Debian they look pretty much the same, not sure if that's the same for everyone though).

It highlights words that are different, rather than the whole line.

You can also set the column width of the terminal to match your current width/columns:

colordiff -y --width=$COLUMNS mylovelyfile1.txt mylovelyfile2.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.