Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Syntax match any 2 spaces at end of line
I'm using the following syntax match to conceal 2 spaces at the end of a line with a special character.
syntax match Normal '\s\{2}$' conceal cchar=⏎
A line with two spaces at the end of it would display like this:
some line with two trailing spaces⏎
However, if there are more than two spaces I don't get the special character. I would expect that this would still match on the last two spaces. For example:
a line with 4 trailing spaces ⏎
Instead, it shows all 4 spaces with no special character.
If I do a normal search for /\s\{2}$
, it will find 2 spaces at the end of the line, even if there are additional preceding spaces. Whereas the syntax match
still matches if other characters proceed 2 trailing spaces, but not if the proceeding characters are spaces.
Why doesn't my syntax match work if there are proceeding spaces? How can I match 2 trailing spaces on a line, even if there are additional spaces before it?
2 answers
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
rcmosher | (no comment) | May 24, 2024 at 12:33 |
A Fix
An existing pattern needs to be cleared to allow the new rule to match on any 2 trailing spaces:
syntax clear markdownLineBreak
syntax match markdownLineBreak '\s\{2}$' conceal cchar=⏎
Why it's not working
The syntax pattern only matches on exactly 2 trailing spaces because there is a standard pattern matching on 2 or more spaces for markdown. See /usr/share/vim/vim90/syntax/markdown.vim
syn match markdownLineBreak " \{2,\}$"
Troubleshooting
I was able to determine the name of the problem match by using the synIDattr()
function on trailing spaces where my match wasn't applied. The following will get the syntax group-name applied to the current position.
synIDattr(synID(line("."),col("."),1),"name")
Utilizing the Existing Pattern
You could also take advantage of the current pattern with something like
hi markdownLineBreak gui=underline cterm=underline
From my understanding you can't add conceal to an existing pattern, but can replace it with matchadd()
. vi.stackexhange answer
0 comment threads
You could explicitly match the other whitespace, too, to see if that gets around an existing rule.[1] Here are two ways to do that:[2]
Match the other whitespace in another rule
You could use a \@=
lookahead to match all the whitespace before your special rule. This might cajole your original rule into matching, since the remainder is back to the last two chars.
syntax match Normal /\v\s(\s{2}$)\@=/
syntax match Normal /\v\s{2}$/ conceal cchar=⏎
One small advantage of this approach is you could set the extraneous whitespace to a different group and highlight it separately to mark it as unnecessary.
Match everything, but start capture at the last two space chars
Vim lets you start the capture late[3] with a \zs
capture start marker. It's possible there are weird interactions between that and conceal
, but barring that it should work.
syntax match Normal /\v\s*\zs\s{2}$/ conceal cchar=⏎
-
As it turns out, there was a rule in the Markdown language syntax that matched all the whitespace at EOL. If there was whitespace that wasn't exactly two characters, the default rule would consume the whole string without giving other rules a chance to match later substrings of consumed characters. ↩︎
-
Please excuse my
\v
modifiers. It's much easier for me to comprehend Vim regex with it. ↩︎
1 comment thread