3

When resolving merge conflicts, a fairly common pattern is that both I and another person have modified a list or some other section of code which commonly gets appended to. Such as:

global.registerFeature(fc);
global.registerFeature(fb);
global.registerFeature(fa);
<<<<<<< 
global.registerFeature(aNewFeature);
=======
global.registerFeature(anotherNewFeature);
>>>>>>> 

When I look at a merge conflict like this in vimdiff, vim gives me options for choosing one or the other. But what I want to do is apply both diffs. I usually just resort to editing the merged file directly (just deleting the merge markers); but is there an easier way to do this in vimdiff?

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
  • Vim only allows you to do `:diffget` or `:diffput`. – romainl Apr 18 '16 at 19:03
  • Shouldn't one of the sections not show up as diff as it's already committed? Then in the diff, you can just keep your changes and delete other blocks showing up as diff. – ronakg Apr 18 '16 at 20:34

2 Answers2

4

To combine changes from both the target and merge branches in a single command:

You can just delete the lines with Git conflict markers. The following two methods will delete all lines that start with:

<<<<<<<
=======
>>>>>>>

Method 1: Manually Entering and Executing a Command

:g/^<\{7}\|^|\{7}\|^=\{7}\|^>\{7}/d

Method 2: Implementing a User Defined Command

"Delete all Git conflict markers
"Creates the command :GremoveConflictMarkers
function! RemoveConflictMarkers() range
  echom a:firstline.'-'.a:lastline
  execute a:firstline.','.a:lastline . ' g/^<\{7}\|^|\{7}\|^=\{7}\|^>\{7}/d'
endfunction
"-range=% default is whole file
command! -range=% GremoveConflictMarkers <line1>,<line2>call RemoveConflictMarkers()

Vim diffget and diffput will only choose one branch or the other. So the only real solution other than the one given above is to manually yank and paste from both files into the working copy.

Community
  • 1
  • 1
user3751385
  • 3,752
  • 2
  • 24
  • 24
-1

I'm not sure exactly what version control system you are using, but here is a guide for using Vim to do merges with Mercurial: https://www.mercurial-scm.org/wiki/MergingWithVim

You should be able to do something similar with whatever you are using, although keep in mind that vimdiff is not really meant for complicated merges, so it will be a bit kludgy. That same page links to the splice plugin, which is supposed to help with doing complex merges.

DrEsperanto
  • 167
  • 9
  • OP says nothing about Mercurial. Irrelevant. Also, vim does support 3 way diffs using Gdiff, which is precisely what OP is referring to. See: http://vimcasts.org/episodes/fugitive-vim-resolving-merge-conflicts-with-vimdiff/ – user3751385 Apr 27 '18 at 15:58
  • Hmm, I agree that mercurial is not directly relevant, but the OP does not say what vcs is used.If I didn't know better I'd say that someone else used my account to make this answer. Memory is strange. – DrEsperanto Apr 28 '18 at 17:47