1

(I know the goals are dumb, and I know I could set ffs=unix,dos to not see ^M; I'm just trying to understand more about vim).

I was playing around with vim keywords, because I want to conceal certain things. I was able to successfully hide a keyword with syntax like this:

syn keyword Windows happy conceal cchar=A
set conceallevel=2

This would successfully create a keyword group called "Windows" that conceals happy and replaces it with A in my text.

I cannot do the same with control characters:

syn keyword Windows ^M conceal cchar=A
set conceallevel=2

Whether I type out ^M or use CtrlVCtrlM, I am unable to conceal a control character.

Nothing in :help syn mentions anything about control characters being excluded (it only says I can't have cchar be a control character).

What am I doing wrong?

1

I cannot do the same with control characters.

When you use syn keyword Vim is actually trying to match keywords, which are defined as sequences of characters included in 'iskeyword'.

From :help :syn-keyword:

Don't forget that a keyword can only be recognized if all the characters are included in the 'iskeyword' option. If one character isn't, the keyword will never be recognized.

You can have that recognize a ^M by adding it to 'iskeyword':

setlocal iskeyword+=13
syn keyword Windows ^M conceal cchar=A
set conceallevel=2

(Note that ^M above is entered as Ctrl+V, Enter.)

But note that syn keyword will only recognize ^M when it appears on its own, surrounded by whitespace, as a keyword, and not when it's right at the end of another word, which would be more typical...

A better solution is to use syn match for this case, which uses a regular expression to define a match. In that case, you can use '\r' as a pattern to match any ^M anywhere in the line:

syn match Windows '\r' conceal cchar=A
set conceallevel=2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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