3

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.

Steven Thiel
  • 153
  • 4

1 Answers1

3

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
romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thank you for providing a very insightful answer, as you always do. I know you are familiar with the XY problem. The way I read the question was: X = figure out if the cursor is in a DiffAdd block, Y = get the highlight group. Out of sheer curiosity, would you know a way to solve X in a more direct fashion? – Friedrich Apr 06 '23 at 06:45
  • 2
    Well, the XY problem is a bit of a plague here on SO, but it doesn't appear to be the case with this question. The way OP tried to solve his problem was kind of wrong (with background colors) be he was generally on the right track because Vim doesn't expose any direct way to answer his question. AFAIK, starting with `diff_hlID()` is the only working method and it is not exactly intuitive, even after reading the related documentation. Note that checking the current highlight group is *relatively* common, for checking if the cursor is in a comment block or something like that. – romainl Apr 06 '23 at 08:13
  • "checking the current highlight group is relatively common" - I wasn't aware of that. Thank you once more. – Friedrich Apr 06 '23 at 08:25
  • Thank you very much for the answer as this does exactly the job and I could not find it! One additional question: Would it in a similar way also be possible to detect a `DiffDelete` block as my cursor cannot move into them but jumps over them? – Steven Thiel Apr 06 '23 at 09:06
  • 1
    No cursor means no information for the highlight group at the cursor, I'm afraid. – romainl Apr 06 '23 at 10:04
  • All right I already thought it would be this way. Thank you nevertheless! – Steven Thiel Apr 06 '23 at 11:13