ignore lines with particular words in vimdiff output

6.4k views Asked by At

I have 2 large files to take vimdiff. In vimdiff output I want to ignore lines showing diff but have a particular word.

e.g. in my case I want to ignore diff of all lines with prefix WARNING: in my files.

Thanks,

3

There are 3 answers

1
pdjota On

Why don't you filter the files before invoking vimdiff?

grep -v "^WARNING" file1 > file1_w; 
grep -v "^WARNING" file2 > file2_w; 
vimdiff file1_w file2_w

If you're using Bash or zsh, you can do it with a single command:

vimdiff <(grep -v "^WARNING" file1) <(grep -v "^WARNING" file2)
4
sanmiguel On

In order to achieve this, you could add the following to your .vimrc (a simple-minded modification from the example found by running :h diffexpr within vim):

set diffexpr=MyDiff()
function! MyDiff()
    let opt = ""
    if exists("g:diffignore") && g:diffignore != ""
        let opt = "-I " . g:diffignore . " "
    endif
    if &diffopt =~ "icase"
        let opt = opt . "-i "
    endif
    if &diffopt =~ "iwhite"
        let opt = opt . "-b "
    endif
    silent execute "!diff -a --binary " . opt . v:fname_in . " " .
        \ v:fname_new . " > " . v:fname_out
endfunction

It's noteworthy that the functionality provided by the -I (or --ignore-matching-lines=) switch for diff will ONLY ignore any changed lines where the line in BOTH (or all) files matches this expression.

See man diff for more details on the --ignore-matching-lines=RE switch and :h diffexpr within vim for more details on this.

EDIT: Added the optional variable g:diffignore to control what pattern to use. Also used function! to force replacement on definition.

4
Wolph On

I've looked for a solution to this for a long time and I found the EnhancedDiff plugin to be the easiest solution which makes your diffs slightly more intelligent to begin with :)

1. Installation (https://github.com/chrisbra/vim-diff-enhanced#installation)

Use the plugin manager of your choice:

  • Pathogen
    • git clone https://github.com/chrisbra/vim-diff-enhanced.git ~/.vim/bundle/vim-enhanced-diff
    • :Helptags (only needed once after the installation to install the documentation)
  • NeoBundle
    • NeoBundle 'chrisbra/vim-diff-enhanced'
  • Vundle
    • Plugin 'chrisbra/vim-diff-enhanced'
  • Vim-Plug
    • Plug 'chrisbra/vim-diff-enhanced'

2. Diff as usual

Using vimdiff, nvim -d or diffthis for example

3. Set the filter

:EnhancedDiffIgnorePat ^WARNING:.*

4. Re-diff

:diffupdate