Gabe Moothart
Gabe Moothart

Reputation: 32082

How do I accept both changes in vimdiff hunk?

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?

Upvotes: 3

Views: 2065

Answers (2)

user3751385
user3751385

Reputation: 4229

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.

Upvotes: 4

DrEsperanto
DrEsperanto

Reputation: 197

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.

Upvotes: -1

Related Questions