5

I'm using Vim with the Vaxe plugin for syntax highlighting, the plugin's highlighting file is here for reference.

I'm trying to add some conceals to tidy up the code visually, but I can't seem to get anything to apply.

The simplest thing I'm trying to achieve as to conceal the word "package" as just the letter "p".

Using various plugins like SyntaxAttr.vim, I determined the syntax attributes for the word package are:

hi<haxeTypeDecl> trans<haxeTypeDecl> lo<Statement>

So I try to add it with:

:syn match haxeTypeDecl /package/ conceal cchar=p

The entry seems to be added as I can see it with ":syn" as:

haxeTypeDecl   xxx abstract interface from import to using enum class package
               match /^class\>/
               match /[^.]\s*\<class\>/ms=s+1
               match /package/  conceal
               links to Keyword

Then I set conceal level to 2, but the text is not concealed. It works when I test it in an empty stand alone document, but doesn't in a real document. I'm guessing I'm not properly overriding the highlighting, but I'm not sure what I'm meant to do. Do I make my own group and linked it with ":hl! link"?, add a match higher priority match with matchadd()? Do I have to also override the Statement group or the Keywords group? Vim's syntax highlighting seems unbelievably dense.

Thank's in advance!

2
  • 2
    Does it change anything if you type :syn match haxeTypeDecl /package/ conceal containedin=ALL cchar=p instead of :syn match haxeTypeDecl /package/ conceal cchar=p (basically just adding the containedin=ALL argument) ?
    – saginaw
    Commented Dec 3, 2015 at 0:54
  • No, not besides the having the tag in the ":syn" menu. Commented Dec 3, 2015 at 3:20

2 Answers 2

5

The problem in your case is that the syntax highlighting script for your package defines a keyword (enter :h syn-keyword for more details) and that always has priority before any of the syn-match or syn-region rules.

In your case, I would therefore simply use a matchadd() call like this:

 :call matchadd('Conceal', 'package', 10, 99, {'conceal': 'p'})
 :set conceallevel=2 concealcursor=nv

This requires a relatively new Vim version (I don't remember the exact patch level, but a 7.4.700 should work)

8
  • This works, thank you, another quick question, is there an easy way to to conceal with multiple characters? For example if I wanted "package" to become "pa", it seems to only use the first character. Commented Dec 3, 2015 at 19:49
  • I don't think this is possible. You could possibly work around that by matching 'pack\zeage' and 'pack\zsage' and use different conceal chars. Commented Dec 4, 2015 at 7:17
  • Can the conceal through call matchadd exist solely for a given filetype? By simply calling the command I get the conceal applied globally.
    – oblitum
    Commented Aug 27, 2016 at 7:37
  • @pepper_chico what do you mean by globally? match functions are window local by default. Commented Aug 27, 2016 at 19:54
  • @ChristianBrabandt I mean that on au FileType purescript call matchadd('Conceal', '\\', 10, 99, {'conceal': 'λ'}), for example, after a open a purescript file, the conceal is active for any other file.
    – oblitum
    Commented Aug 27, 2016 at 20:05
2

Based on the discussion I've started in the comments section of the Christian Brabandt answer, I'll leave here this alternative solution for anyone interested in a filetype specific approach:

set conceallevel=2
au FileType purescript set concealcursor=vin
au WinEnter,BufEnter,BufRead,FileType,Colorscheme *
    \ if exists('w:lambda_conceal')                                                                  |
    \     call matchdelete(w:lambda_conceal)                                                         |
    \     unlet w:lambda_conceal                                                                     |
    \ endif                                                                                          |
    \ if &ft == 'purescript'                                                                         |
    \     let w:lambda_conceal = matchadd('Conceal', '\\\%([^\\]\+→\)\@=', 10, -1, {'conceal': 'λ'}) |
    \     hi! link Conceal Operator                                                                  |
    \ endif

The previous sample will conceal \ with λ in the Purescript filetype solely, constrained to anonymous lambdas and if there's a character present afterwards. It also links the Conceal highlighting group with the Operator group so that it looks like an operator.

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.