18

Actually I'd like to display -> with (there is a space after the arrow) in haskell files. But I have the impression the conceal mechanism only work to replace -> by one character. An undesirable effect is visually bad indentation.

Is there a way to achieve this?

Thanks.

Edit: Actually I use this, (from haskell.vim (conceal enhancement) plugin)

syntax match hsNiceOperator "<-" conceal cchar=←
5
  • What is your way to replace one character with another?
    – user256497
    Nov 29, 2011 at 11:26
  • I edited my answer, actually I use the conceal mecanism used for syntax highlighting
    – yogsototh
    Nov 29, 2011 at 11:41
  • can you post the compisition of "→ " - it appears to be essential to the question?
    – sehe
    Nov 29, 2011 at 12:30
  • @sehe I just mean to display two characters by two characters and not only one unicode char. I don't really understand what you mean by composition.
    – yogsototh
    Nov 29, 2011 at 12:38
  • Did you ever find a solution for this? @yogsototh
    – Michael
    Oct 11, 2016 at 12:09

2 Answers 2

21

I do exactly what you want in C. The trick is to conceal each character separately, like so:

syn match ArrowHead contained ">" conceal cchar=▶
syn match ArrowTail contained "-" conceal cchar=─
syn match ArrowFull "->" contains=ArrowHead,ArrowTail

You might find that ArrowHead or ArrowTail gets matched outside an ArrowFull, unfortunately. This is because existing syntax rules use contains=ALLBUT,... or something similar.

To fix this in C, I added ArrowTail and ArrowHead to the cParenGroup cluster, which seems to prevent any problems.

syn cluster cParenGroup add=ArrowTail,ArrowHead

You may need to do something similar for Haskell.

Since I don't use the conceal feature at all otherwise, I tell Vim to go ahead and "conceal" the arrows ALL the time:

set conceallevel=1 concealcursor=nvic

BTW if you don't like the default colors for the conceal chars, you can change them like this:

hi conceal ctermfg=DarkBlue ctermbg=none guifg=DarkBlue guibg=none
2
  • "because existing syntax rules use contains=ALLBUT,... or something similar." : use containedin=ArrowFull at end of syn match ArrowHead [...] and syn match ArrowTail [...], then. Feb 13, 2015 at 10:02
  • @user2987828 This doesn't work for me in Vim 7.4. I get false positive ArrowHeads and ArrowTails inside #defines.
    – superjer
    Feb 16, 2015 at 23:17
5

Super-old question, but in case anyone else is trying to figure this out, I have had success with the following:

call matchadd('Conceal', '<-\&<', 10, -1, {'conceal':'←'})
call matchadd('Conceal', '<\zs-', 10, -1, {'conceal':' '})
hi Conceal        ctermbg=NONE ctermfg=red guifg=red

The second argument of matchadd is <pattern>. Looking at :help pattern tells us that a pattern is one or more branches separated by \|, and a branch is one or more concats separated by \&. From the docs: "[a branch] matches the last concat, but only if all the preceding concats also match at the same position."

So in the first call we specify a pattern with one branch (there are no \| values), made of two concats. The first concat matches the full arrow, and the second matches the first character of the arrow. Thus, the last concat is the < which is all that is taken as the match for the conceal parameter, but this only matches if the < appears in a <-. The second call gives a pattern with one branch and one concat. The pattern begins with the < but the match is reset with the zero-width atom \zs which resets the match at the next character, the -, which is concealed with a space. The hi Conceal is just highlighting.

In my testing < by itself doesn't get turned into and - by itself isn't turned into a space.

Hope this helps someone :)

Your Answer

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

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