vimvimdiff

vimdiff: Is there a possibility to find out if the cursor is in a DiffAdd block?


I'm trying to find out in a script if the current cursor position is a block of lines that were added. Is this somehow possible using vim script?

So far I tried to match for the background color of the current line and the highlight group but without success.


Solution

  • I would like to point you to a place in the documentation and call it a day but that kind of task, specifically, is not super easy to figure out.

    So… internally, highlight groups have numerical IDs associated to them, which makes the whole mechanism kind of opaque, but you can find variants of the following command floating around the internet, that make it somewhat easier to identify the highlight groups affecting the text under your cursor:

    :echo synIDattr(synID(line("."), col("."), 1), "name")
    

    See :help synid() and :help synIDattr().

    The problem with this, is that Vim makes a difference between two kinds of highlight groups: those that concern the syntax of your text and those that concern the UI of the editor. This is a problem because synID() only works on syntax highlight groups and DiffAdd is a UI highlight group.

    But you are certainly not the first user who needs that because there is a built-in function, :help diff_hlID(), for that exact purpose:

    :echo synIDattr(diff_hlID(line("."), col(".")), "name")
    DiffAdd