Question

Is there anyway to have vim display something that is different from the actual text in a file?

For example, is it possible to make it so that when a file contains:

<a href="http://www.example.com">link</a>

Vim displays

[link]

Thanks in advance.

Was it helpful?

Solution

Yes, it is possible. Writing a vim syntax file is a little arcane, so you might look first to see if someone has already written something that does what you want. For example, try http://vim.wikia.com/wiki/Patch_to_conceal_parts_of_lines#Using_Conceal. (If the fragment gets lost, then scroll down to the section on "Using Conceal". A quick search on Google and on GitHub did not turn up anything better.) The starting point in the docs is

:help conceal

or back up a little and start reading at :help :syn-arguments.

This feature was quite controversial when it was first introduced. Initially, Bram refused to include it, but now the syntax file for vim help files in the standard distribution uses the feature.

I did some experimentation. Try this. You can always add syntax rules; the latest wins. So as long as this gets :sourced after the regular HTML syntax file, you should be good. Remember to set 'conceallevel' to something other than the default, and that 'concealcursor' matters for the current line.

syn region htmlLink matchgroup=htmlLinkTag start="<a\>\_[^>]*>" end="</a>" contains=@Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,javaScript,@htmlPreproc
syn match htmlLinkTag "<a\>\_[^>]*>" conceal cchar=[
syn match htmlLinkTag "</a>" conceal cchar=]

I am a little afraid that this might highlight too much with the htmlLinkTag group.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top