2

I'm using vimwiki plugin with markdown. I'd like to highlight text using double equals, which isn't standard, but is quite useful. Ideally, this should replace background (for instance, yellow) and keep any other formatting, such as bold or italics.

Normal text. ==Highlighted text with **bold** and *italics*==. Normal text

Should be:

Highlighted text with bold and italics

3
  • 2
    The standard option is to define a syntax item for it (:help syntax, maybe?) and the highlight def link it to a group
    – D. Ben Knoble
    Oct 30, 2020 at 22:34
  • what is the expected behavior when both bold and italic occurs like *italic with **bold** inside* « italic with bold inside » ? There is no bolditalic in Vim's highlight feature. Nov 2, 2020 at 9:50
  • This is precisely one extra reason to try this "highlighting" solution: provided one text with italics inside, you can't simply surround it and make it bold, as it would lose visual formatting.
    – Ludenticus
    Nov 2, 2020 at 15:07

1 Answer 1

3

I had the same question. My solution is not perfect, since I do not do exactly what you want, but you can adapt my solution to yours, if you are willing to add more highlight groups.

To do close what you want, make the following after/syntax/vimwiki.vim file (I guess if you are just doing markdown, you could make it after/syntax/markdown.vim):

highlight MDHighlightH term=standout ctermfg=0 ctermbg=11 guifg=orangered guibg=yellow2
highlight MDHighlightHB term=standout ctermfg=0 ctermbg=11 guifg=orangered guibg=yellow2 cterm=bold

syntax region MDHighlight matchgroup=HighEnd start='==' end='==' concealends
highlight link MDHighlight MDHighlightH                                                                                                    

syntax region MDBoldHighlight matchgroup=HighEnd start='\*\*' end='\*\*' concealends containedin=MDHighlight contained
highlight link MDBoldHighlight MDHighlighHB

First, I make two new highlights. The first is identical to the Todo highlight. The second adds bold. (To see all the highlights that are currently available and their definitions, do :highlight. To see all the ones currently active, do :so $VIMRUNTIME/syntax/hitest.vim)

Next, I make the syntax groups. The first one is for a highlight surrounded by ==, and then it conceals the ==. It is then linked to the first new highlight group that I made. The second is for bold that is contained within the highlight group. Again, the ** are concealed, and it is linked to the second highlight group.

I did not do the italic one, but you can easily extend this for yourself.

However, I did not really like the result. The bold in the highlight does not actually make it stand out more. So instead, I went with this:

syntax region MDHighlight matchgroup=HighEnd start='==' end='==' concealends
highlight link MDHighlight Title

syntax region MDBoldHighlight matchgroup=HighEnd start='\*\*' end='\*\*' concealends containedin=MDHighlight contained
highlight link MDBoldHighlight Todo

The Title highlight group basically makes it yellow (for me). And then the Todo makes it like a highlight. So the "first level" of highlighting for me just changes the font color, and then the bold highlights it. I think this is better because the bold highlight actually stands out more.

This biggest drawback is that if you have any other vimwiki syntax within your highlight, it will break the conceal of that. For instance, if you highlight a vimwiki link, it won't conceal it (where it shows only the title when your cursor is on another line), and the link color won't work. It will only keep the highlight syntax that you define. This is because it is in the after folder in vimwiki. (Putting it in syntax/vimwiki.vim also breaks the link conceal.) I started to look through vimwiki to see how the link conceal works so that I could add it to my file, but it is very complicated, so I abandoned it. The links still work---if you hit Enter on the link, you still go to that file. It is only the conceal that does not work.

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.