Hot answers tagged

124

vim has this functionality built in (with the correct command line flag). vim -d <file1> <file2> This opens each file in a view and highlights the differences. Any code that is identical is folded away so you do not need to look at identical code or scroll through huge chunks of identical code. But there is also a wrapper application vimdiff ...


30

There is DirDiff.vim plugin (GitHub) to diff and merge two directories recursively. It performs a recursive diff on two directories and generate a diff "window". Based on that window you can perform various diff operations such as opening two files in Vim's diff mode, copy the file or directory recursively to the other, or remove the directory tree ...


22

You can use Ctrlw-x. From :he CTRL-W_x: CTRL-W x CTRL-W_x CTRL-W_CTRL-X CTRL-W CTRL-X Without count: Exchange current window with next one. If there is no next window, exchange with previous window. With count: Exchange current window with Nth window (first window ...


22

To go along with @janos's answer, you can set an autocmd to automatically press those keys when the window is resized (put this in your .vimrc without the leading colon if you want it to apply every time you open Vim): :autocmd VimResized * wincmd = Here are the docs for the VimResized autocmd.


21

You can equalize the size of windows with <c-w>=.


18

I normally use git on the command line. But when there is a merge conflict I use Vim to resolve them with (personally I do it with with the fugitive plugin). Note: fugitive is good for a lot of git manipulation from within Vim. My favorite feature is the 3 way diff of a merge conflict. git supports this in vimdiff via git mergetool. I have installed ...


15

I don't think this can be done by Vim itself. From :h view-diffs (emphasis mine): hl-DiffText DiffText Changed text inside a Changed line. Vim finds the first character that is different, and the last character that is different (searching from the end ...


14

You can create an expression mapping that checks &diff. This is done using the <expr> keyword. Example nnoremap <expr> <C-J> &diff ? ']c' : '<C-W>j' With this, each time you type <C-J>, the expression &diff ? ']c' : '<C-W>h' is evaluated, and the result (a string) is used for your map. So, in diff-mode, <...


13

Ctrlw= will resize the split panes to become equal. But I don't know how to make them equal automatically, triggered when the window is resized.


13

If you are editing an open file and want to compare it to another file without closing the current one: Open the new file in split screen: For vertical split: :vs otherFile or horizontal split: :split otherFile Switch cursors to different split screen: ctrl+w ctrl+w Invoke "diff mode" in file: :diffthis Switch to other file and invoke "diff mode": ...


13

I quite like the diff colors in my colorscheme:


13

As Christian Brabandt suggested in a comment, this can be caused by the 'cursorbind' setting. From :help 'cursorbind': When this option is set, as the cursor in the current window moves other cursorbound windows (windows that also have this option set) move their cursors to the corresponding line and column. You can use :verbose set cursorbind? to ...


13

The colors are controlled by these four highlight groups (:help hl-DiffAdd): DiffAdd diff mode: Added line DiffChange diff mode: Changed line DiffDelete diff mode: Deleted line DiffText diff mode: Changed text within a changed line These are typically defined by a color scheme, but you can customize them in your ~/.vimrc (after the :colorscheme ...


11

:help diff covers this: In your .vimrc file you could do something special when Vim was started in diff mode. You could use a construct like this: if &diff setup for diff mode else setup for non-diff mode endif


10

As you would switch any other window, <c-w>x or <c-w>r are two options. Having only two windows opended <c-w>k will switch them and leave the cursor in the window where it was before the switch (i.e. if before the switch the focused window is on the left, after the switch it will be on the left). <c-w>x will switch the windows and ...


10

One quick fix is to disable syntax highlighting. Sometimes the code syntax highlighting will cause the foreground text to be the same color as the vimdiff background color, making the text "invisible". :syntax off If you want to automatically do this for vimdiff, then add this to the end of your ~/.vimrc: if &diff syntax off endif


10

You can use the &diff option like in your vimrc: if &diff echo "in diff mode" else echo "not in diff mode" endif See :h 'diff'


9

I have a syntax/diff.vim (that I source manually) with the following contents: hi DiffText cterm=none ctermfg=Black ctermbg=Red gui=none guifg=Black guibg=Red hi DiffChange cterm=none ctermfg=Black ctermbg=LightMagenta gui=none guifg=Black guibg=LightMagenta


8

You can do this: :bufdo diffoff The :bufdo command runs a given command on all buffers. It works by cycling through all buffers and executing the command, so you will end up on the last buffer. Caveat From the help: Note: While this command is executing, the Syntax autocommand event is disabled by adding it to 'eventignore'. This considerably speeds ...


8

I used the trick from this SO question to see what the command line was, and got this: :exe '!tr "\0" " " </proc/' . getpid() . '/cmdline' :!tr "\0" " " </proc/23932/cmdline gvim -f -d -c wincmd J foo ./foo_LOCAL_23800 ./foo_BASE_23800 ./foo_REMOTE_23800 So the trick is using Ctrl-WJ (via :wincmd): :...


7

You have a few questions, some explicit and some implicit. I'll try to answer them in order: What Do These Commands Do? You're running two commands: highlight DiffChange cterm=none ctermfg=fg ctermbg=Red gui=none guifg=fg guibg=Red highlight Normal term=none cterm=none ctermfg=White ctermbg=Black gui=none guifg=White guibg=Black Both of these commands ...


7

One solution, as suggested by muru (thanks), is to use the diffchar.vim plugin and setting the let g:DiffUnit="Word1". This plugin should be used in the standard vimdiff session.


7

I think the most likely culprit is another buffer still in diff-mode. This can happen if you're looking at multiple diffs, one after another, and don't turn off diff-mode between diffs. Disable diff-mode in irrelevant buffers The solution is to find the offending buffer(s) and turn off diff-mode with :diffoff. If you don't want to search for it, you can ...


7

If you want the window widths to remain equal as you change the size of the Vim window, try this in your ~/.vimrc: if exists("##VimResized") if &diff au VimResized * wincmd = endif endif


7

You could add this to your .vimrc: command! -nargs=1 -complete=file Diffsplit diffsplit <args> | wincmd p This create a Diffsplit command (note the capitalized D) which takes one argument and accept file names as completion. The command will create the diffsplit with the argument provided and use the command windcm p to go back to the initial file. ...


6

This is controlled with the shortmess option; specifically, set shortmess=Ot seems to do the trick for me. Meaning of the Ot flags: O message for reading a file overwrites any previous message. Also for quickfix message (e.g., ":cn"). t truncate file message at the start if it is too long to fit on the command-...


6

]c doesn't jump to the first difference, it jumps to the next difference. If your first difference is on the first line, ]c would jump to the the second one which is not what you want. One could work around that with ]c[c (jump to next difference then jump to previous). This command: $ vim -d -c 'norm ]c[c' filea fileb seems to do what you want. I don't ...


6

Vim's -o and -O options will open the files while splitting them horizontally or vertically respectively. Taken from Vim's help page :help -o and :help -O: *-o* -o[N] Open N windows, split horizontally. If [N] is not given, one window is opened for every file given as argument. If there is not enough room,...


6

DiffAdd and DiffDelete are only used when actually performing a diff ($ vim -d foo.txt bar.txt, $ vimdiff foo.txt bar.txt, $ git mergetool, etc.); those highlight groups are totally irrelevant, here. The highlight groups used for added and deleted lines when reading a diff file are diffAdded and diffRemoved. Adding the lines below between line 272 and line ...


6

Since there was nothing explicit in my .vimrc that acted on only-whitespace lines and the diff syntax file did no such thing either, this had to with my plugins somehow. I find out that I had an EditorConfig file in this project, and in it it had these lines: [*] # other #stuff trim_trailing_whitespace = true I then subsequently added a section for diff ...


Only top voted, non community-wiki answers of a minimum length are eligible