0

This is mostly a curiosity-driven question.

I can't think of anything shorter than x\@=y, which tries to match x and y at the same position. A slightly longer, but probably more self-explaining pattern is \_.\@!, which reads more or less as "a thing that matches everything, should not match".


For those who wonder why I need a pattern matching nothing and nowhere, I need one to get a match ID for matchadd, so that I can then update that very match over and over, reusing always the same id. Basically, I call this

let g:id = matchadd('Search', 'x\@=y', 10, -1)

when loading a plugin, and later on everytime I need to updated the matches I've created I can do

call matchdelete(g:id)
call matchadd('Search', 'some pattern', 10, g:id)
11
  • I don't understand why you need to get a global matchid for something that does not match Jul 1, 2023 at 9:25
  • @ChristianBrabandt, call matchadd(_, _, _, id) errors if id is already taken, so I have to call matchdelete(id) first, but if I don't have one, it errors.
    – Enlico
    Jul 1, 2023 at 9:27
  • So why do you only call matchdelete() if you have an existing match-id? Jul 1, 2023 at 9:28
  • .... Because matchadd fails if I call it with an existing id.
    – Enlico
    Jul 1, 2023 at 9:29
  • .. so if you don't have an existing id, use -1 and you will get a new fresh id. Jul 1, 2023 at 9:31

1 Answer 1

4

You've gotten a better answer in the comments, but for curiosity's sake, note that unless you add atoms specifically to avoid this, patterns will generally only match in a single line.

So there are a large number of two character patterns that don't match anywhere:

  • x^ An x followed by the start of a line,
  • $. The end of a line followed by any character,
  • $^ The end of a line followed by the start of the line,
  • etc.
1
  • I didn't even try that, thinking ^ and $ would be literal if not used at the beginning and end of the pattern! Apparently, I was confusing with SED: sed 's/^c/X/' <<< 'ci^ao' gives Xi^ao, but sed 's/i^/X/' <<< 'ci^ao' gives cXao.
    – Enlico
    Jul 3, 2023 at 15:40

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.