4

I wish to fully conceal markdown links, even if they have extensions. For example:

[Anchor text](http://whatever...)

[Anchor text](http://whatever...){:rel="nofollow noopener noreferrer" target="_blank"}

Should become:

Anchor text

I can conceal the () and its content, and the {} and its content. However, I can't hide the characters [].

Moreover, concealing all this long text sometimes generates a ghost line below, because it's really there, concealed! Is there a way to hide the gost newline?

This is my code:

    autocmd Filetype markdown,liquid,text
                \ syn region markdownLink matchgroup=markdownLinkDelimiter
                \ start="(" end=")" keepend contained conceal contains=markdownUrl
    autocmd Filetype markdown,liquid,text
                \ syn match markdownExt /{[.:#][^}]*}/ conceal contains=ALL

My result is:

[Anchor text]

11
  • 1
    The vim-markdown plugin allows to conceal links. Commented Aug 13, 2020 at 10:37
  • Yes, I checked its code to do the mine and it was helpful. However, it does not conceal the extensions and it uses scopes in coherence with its own syntax definitions, while I prefer to use the standard syntax of vim. I also checked mkdx plugin, which was helpful for me as well.
    – Unix
    Commented Aug 13, 2020 at 12:30
  • 2
    Welcome to Vi and Vim!
    – D. Ben Knoble
    Commented Aug 13, 2020 at 13:58
  • @D.BenKnoble, thanks! :)
    – Unix
    Commented Aug 13, 2020 at 14:11
  • 2
    @Gerard what do you mean with the ghost line? If this is about wrapping earlier than expected, than it is not possible to change. Vim will always wrap on the line content and not about the visual representation of the line. Commented Aug 13, 2020 at 14:37

1 Answer 1

4

One approach is to overwrite the existing markdownLinkText syntax item with one that conceals the [ and ] delimiters:

This item is currently defined with the following line from $VIMRUNTIME/syntax/markdown.vim (line continuations added by me for Stack Exchange formatting purposes):

syn region markdownLinkText matchgroup=markdownLinkTextDelimiter
    \ start="!\=\[\%(\_[^]]*]\%( \=[[(]\)\)\@=" end="\]\%( \=[[(]\)\@="
    \ nextgroup=markdownLink,markdownId skipwhite
    \ contains=@markdownInline,markdownLineStart

This highlights the ends of the region (i.e. the [ and ] delimiters) with the markdownLinkTextDelimiter group via matchgroup. To instead make it conceal them, tack concealends onto the end:

syn region markdownLinkText matchgroup=markdownLinkTextDelimiter
    \ start="!\=\[\%(\_[^]]*]\%( \=[[(]\)\)\@=" end="\]\%( \=[[(]\)\@="
    \ nextgroup=markdownLink,markdownId skipwhite
    \ contains=@markdownInline,markdownLineStart
    \ concealends

The downside of this approach is that if the existing syntax definition is altered to handle more cases, then you may need to update your tweaked version, too. I'm guessing the markdown link syntax is reasonably stable at this point, so hopefully this will work well for you.

This will take precedence over the existing item so long as it is defined later, so putting it .vim/after/syntax/markdown.vim is the best place for it. (Putting it in a FileType autocommand should work too, but there are caveats.)

14
  • 1
    Exactly, it works perfect! With reason I was not able to implement it, because it's difficult. I normally add this things in my .vimrc file. Should I add this into the folder ~/.vim/after/syntax/markdown.vim?
    – Unix
    Commented Aug 14, 2020 at 13:13
  • 1
    @Gerard Yes, ~/.vim/after/syntax/markdown.vim seems to be the appropriate place for this override.
    – filbranden
    Commented Aug 16, 2020 at 18:53
  • Thanks, @filbranden. I moved the syntax definitions in that file, however, my markdownExt is not working. It worked before, probably by mistake? If I have to fix something, I don't know what it is. At the moment I'm reading :syn-match, still I'm not sure if it's the syn-region...
    – Unix
    Commented Aug 16, 2020 at 22:49
  • @Gerard Do you mean markdownLinkText?
    – filbranden
    Commented Aug 16, 2020 at 22:52
  • 1
    @filbranden, fixed! I had defined the same without concealing in my own colorscheme (vim-atomic), so it was not concealing...
    – Unix
    Commented Aug 18, 2020 at 20:49

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.