Search code examples
regexvimvim-syntax-highlighting

Vim regex for git merge conflicts


Im making a plugin that will add syntax groups for files with merge conflicts in vim.

An example conflict is shown below.

<<<<<<< HEAD
ourselves
=======
themselves
>>>>>>> deadbeef0123

The current regex for the <<<<<<< HEAD to ======= is working correctly.

syntax region conflictHead containedin=ALL start=/^<<<<<<< \@=/ end=/^=\@=/

But Im stuck trying to get the right regex for everything after =======

syntax region conflictMerge containedin=ALL start=/^=======$\@<!/ end=/^>>>>>>> \@=/

enter image description here

The regex should get everything after the separator and up until the commit sha.

Any tips/pointers?


Solution

  • In your command:

    syn region conflictMerge containedin=ALL start=/^=======$\@<!/ end=/^>>>>>>> \@=/
    

    Maybe you could replace the pattern:

    ^=======$\@<!
    

    With:

    ^=======\zs$
    

    On my machine, it seems to do what you want, that is highlighting the text after ======= until the next >>>>>>>.

    If it works and you want to better understand \zs, see :h \zs. Basically, it excludes everything before from the match, and uses it as a positive assertion (what is before must be present before the match in which you're really interested).