To get the highlight group that a group links to, you can use this function:
function! GetLinkTarget(group)
let l:groupID = hlID(a:group)
" Get the group this highlight group links to
let l:linkedGroupID = synIDtrans(l:groupID)
let l:linkedGroupName = synIDattr(l:linkedGroupID, 'name')
echom a:group . " → " . l:linkedGroupName
endfunction
Example usage:
:call GetLinkTarget("vim9Comment")
Output:
vim9Comment → Comment
Explanation:
hlID
gets the ID of the highlight group whose linked to highlight group we want to find out.synIDtrans(l:groupID)
gets the ID of the linked to group.synIDattr(l:linkedGroupID, 'name')
translates the ID of the linked to group to its name.