Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

+4
−0

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?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Can you reproduce this with no other syntax rules in the buffer (you can clear the current buffer wit... (5 comments)

2 answers

+3
−0

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

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

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=⏎

  1. 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. ↩︎

  2. Please excuse my \v modifiers. It's much easier for me to comprehend Vim regex with it. ↩︎

  3. Or end the capture early with \ze ↩︎

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »