vimtricks.wiki Concise Vim tricks, one at a time.

How do I ignore whitespace changes when using Vim's diff mode?

Answer

:set diffopt+=iwhite

Explanation

When comparing files in Vim's diff mode, whitespace-only changes (extra spaces, changed indentation) can clutter the diff and hide meaningful edits. Adding iwhite to the diffopt option tells Vim to ignore differences in the amount of whitespace, so you can focus on actual code changes.

How it works

  • :set diffopt+=iwhite — appends the iwhite flag to the existing diffopt settings
  • iwhite stands for "ignore white" — it ignores changes in the amount of whitespace
  • Lines that differ only by whitespace will no longer be highlighted as changed
  • The diff will update immediately after setting this option

Note that iwhite ignores differences in the amount of whitespace, not all whitespace. A line with whitespace versus a line without any whitespace will still show as different.

Example

With two files open in diff mode:

# File 1                    # File 2
if (x == 1) {               if (x == 1) {
    return true;                 return true;
}                           }

Before iwhite: the indentation difference on return true; is highlighted.

After :set diffopt+=iwhite: the diff shows no changes, since only whitespace differs.

Tips

  • To also ignore blank lines, use :set diffopt+=iblank (Vim 8.1+)
  • Use :set diffopt+=algorithm:patience for better diff output on restructured code
  • Reset with :set diffopt-=iwhite to see whitespace changes again
  • Add to your vimrc for permanent use: set diffopt+=iwhite

Next

How do I add more steps to an existing macro without re-recording it from scratch?