Question

When diffing 2 files in VIM, I prefer the lines to be wrapped. However, vimdiff sets wrap to off by default.

Is there a way to set line wrap automatically for every diff?

Was it helpful?

Solution

I use the following:

autocmd FilterWritePre * if &diff | setlocal wrap< | endif

FilterWritePre is triggered immediately before a generated diff is written to the buffer, and setlocal wrap< copies the global value of wrap. Of course it's also possible to simply force setlocal wrap.

OTHER TIPS

You can put the following in your vimrc.

au VimEnter * if &diff | execute 'windo set wrap' | endif

When vim is already started, the commands to start a diff and a wrap are, on each of the split window files:

:diffthis
:set wrap

However, the diff+wrap is improperly displayed:

If you have a long line on one file, and a short or missing line on the other, then the long line will occupy, say, 2 lines on the first side, by the short or missing line will only occupy 1 line on the second side.

This results in a vertical shift of the 2 files. Sure, the colors help to visually compensate this shift. But over several lines of diff, the shift may be higher than the height of the window, making diff unreadable.

So the question is not fully solved... Please advise.

You can run this

vimdiff -c 'set wrap' -c 'wincmd w' -c 'set wrap' file1 file2

I had to solve this question for a demo where I wanted vimdiff to wrap only when launched from a certain terminal.

I finally resorted on the crude:

alias vimdiff='vimdiff +"windo set wrap"'
vimdiff file.orig file

If you have more files, and you don't want to make this permanent in your vimrc

vimdiff -c 'windo set wrap' file1 file2 file3 [file4]

or just windo set wrap once vim is open

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top