0

I'm trying to follow a link using <CR> in a markdown file.

I was giving a look into the native markdown syntax file vim ships with and found a few that use the syn command syntax

syn region markdownUrlTitle matchgroup=markdownUrlTitleDelimiter start=+(+ end=+)+ keepend contained

Is there any way to use a similar matching syntax and maybe pipe the match to a variable?

Thanks in advance

5
  • Welcome to Vim :-) I'm not sure I understand what you want to achieve or maybe I'm misled by the use of the syntax feature. The syntax feature is used for colorization but I understand that you would like to map <CR> to an action that would apply to a markdown link (like in vimwiki) Commented Mar 2, 2023 at 17:21
  • Hi! Sorry for not being more clear, I want to match and return the string of a given pattern, in this case a link, exactly the one you mentioned, similar to vimwiki, so when I press Enter, I can match this string. Commented Mar 2, 2023 at 17:47
  • But instead of using the match() in vimscript, I was wondering if it was possible to use a similar method to the one I mentioned, using syn, start and end for example. Commented Mar 2, 2023 at 17:48
  • You can use match but may be you could also use the result of syntax and catch the part of the text that is assigned to the highlighting group. Commented Mar 2, 2023 at 18:00
  • Yeah I tried but unfortunately it seems the syntax cannot be used in the way I'm thinking. I'll look into the more well known markdown plugins like vimwiki, wiki.vim and vim-markdown. Let's see how it goes haha.But anyway, thank you for your help! Commented Mar 2, 2023 at 18:16

1 Answer 1

0

Here is a function that return the text of the highlighting text if it match the argument:

function! GetSynWord(syname)
  let l:line = line('.')
  let l:col = col('.')

  let l:stack = synstack(l:line, l:col)
  if len(l:stack) == 0
    return ''
  endif

  let l:syname = map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')[0]
  " echo l:syname
  if l:syname != a:syname
    return ''
  endif

  let l:id = l:stack[0]

  let l:start = l:col
  while l:start >= 0 
    let l:nstack = synstack(l:line, l:start)
    if len(l:nstack) == 0
      break
    endif

    let l:nid = l:nstack[0]
    if l:nid != l:id
      break
    endif

    let l:start = l:start - 1
  endwhile

  let l:end = l:col
  let l:sline = getbufline(bufnr(), l:line)[0]
  while l:end < (len(l:sline) + 1)
    let l:nstack = synstack(l:line, l:end)
    if len(l:nstack) == 0
      break
    endif

    let l:nid = l:nstack[0]
    if l:nid != l:id
      break
    endif

    let l:end = l:end + 1
  endwhile


  return l:sline[l:start: l:end - 2]
endfunction

The GetSynWord('markdownLink')[1:] will give you the markdown url if your cursor is on a markdown link.

2
  • Amazing! This is exactly what I needed. Thanks! Commented Mar 2, 2023 at 20:00
  • Thanks for the feedback :-) Commented Mar 2, 2023 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.