Way to configure vimdiff to ignore ALL whitespaces

I’m currently using the vim -d file1 file2 command to compare two files in Vim and it’s working well. However, I’m interested in ignoring whitespace changes since they don’t matter in my source code files.

According to Vim’s documentation, there’s a command that should help with this: set diffopt+=iwhite. But it seems that this command only adds the -b option to the diff tool’s command line, which only ignores trailing whitespaces.

What I actually need is the -w option to ignore all whitespace changes. I can’t seem to find a way to directly modify the diff command line from within Vim.

I know I can compile a custom diff or replace it with a script like diff.sh, but that doesn’t seem like an elegant solution.

Is there a better way to configure Vim to interact with the diff tool and display file differences while ignoring all whitespace changes?


Understanding and Modifying Vim’s Diff Behavior

When using Vim to compare files with the vim -d file1 file2 command, it’s a powerful way to spot differences between two files.

However, there are times when you’d like to ignore whitespace changes, especially when dealing with source code files where such differences might not be relevant.

Vim offers the diffopt option to customize the behavior of its diff mode, but it may not be immediately obvious how to achieve this.

The set diffopt+=iwhite Command

As mentioned in your question, Vim’s documentation suggests using the set diffopt+=iwhite command to ignore whitespace changes.

However, this command does not accomplish what you might expect. It only adds the -b option to the diff tool’s command line, which ignores trailing whitespaces but not all whitespace changes. To ignore all whitespace changes, you need the -w option.

The Challenge: Modifying the Diff Command Line

The real challenge here is how to directly modify the diff command line from within Vim.

While Vim provides various options and settings, it doesn’t offer a direct way to customize the command line arguments of the external diff tool it uses.

Exploring Alternative Solutions

  1. Custom Diff Tool: One option is to compile a custom diff tool that behaves exactly as you desire, incorporating the -w option to ignore all whitespace changes. While this is a viable solution, it might be considered a bit of an overkill for this problem.
  2. Wrapper Script: Another approach is to create a wrapper script for your diff tool. For instance, you could create a diff.sh script that simply calls your preferred diff tool with the -w option. This way, Vim will use your custom script as the diff tool, effectively achieving the desired behavior.

Here’s an example of a diff.sh script:

/path/to/your/diff/tool -w "$@"

Make sure to replace /path/to/your/diff/tool with the actual path to your diff tool.

Setting Up Vim to Use the Wrapper Script

Once you have your wrapper script in place, you can configure Vim to use it. You can set the diffexpr option in your Vim configuration to point to your diff.sh script.

set diffexpr=MyDiff()
function MyDiff()
  let opt = ""
  if &diffopt =~ "icase"
    let opt = opt . "-i "
  if &diffopt =~ "iwhite"
    let opt = opt . "-w "
    let opt = opt . "-b "
  silent execute "!/path/to/your/diff.sh " . opt . v:fname_in . " " . v:fname_new . " > " . v:fname_out

Replace /path/to/your/diff.sh with the actual path to your diff.sh script.

Explanation of diffexpr

  • We set the diffexpr option to define a custom diff command.
  • MyDiff() is a custom function that constructs the diff command line.
  • We check the diffopt option to determine whether to include the -w or -b option.
  • We use the execute command to run our custom diff command.


While Vim’s set diffopt+=iwhite command might not provide the expected behavior, you can work around it by creating a wrapper script and configuring Vim to use it via the diffexpr option.

This way, you can seamlessly compare files in Vim while ignoring all whitespace changes, making your source code comparisons more accurate and efficient.

Related Posts: