2

As title, specifically, is it correct that one of the backslashes in the following regex is redundant in vimscript:

syntax match MyGroup "[a-zA-Z0-9_:\\]"

I tried to ask Copilot but while it said it's redundant it insisted on using double backslashes: So I'm here for the real intelligence: Human Being.


(I turned it into a smaller but clickable image so I you're really interested in what they said just click it.)

3
  • 3
    How about going by the actual documentation instead of asking a AGI: :h syn-match -> :h syn-pattern -> :h pattern and :h /\] and related documentation. Feb 26 at 14:23
  • @ChristianBrabandt: Thanks for the hint, I will read it later today. I didn't know the jargon and to save time I decided to use Copilot. Feb 26 at 14:29
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the solution. It allow the question to rest :-) Feb 27 at 9:10

2 Answers 2

1

You need both \:

  • [a-zA-Z0-9_:\\] match with a, Z, 5, : and \ among other things (but not ])

  • [a-zA-Z0-9_:\]] match with a, Z, 5, : and ] among other things (but not \)

  • [a-zA-Z0-9_:\] is invalid :-)

Remark: The quotes " are pattern delimiter they could be replaced by the standard / or by ' without having an impact on how the pattern is interpreted (since the patterns doesn't contains any ", ' or /).

4
  • Hum... A single `\` should work, with the right quote character though: see vi.stackexchange.com/questions/9706/… I haven't tested it. And I have a doubt. Feb 26 at 13:17
  • @LucHermitte Wow, thanks for mentioning the subtleties. I haven't considered that. Now the problem seems more dizzy for me. Feb 26 at 13:36
  • Am I correct? Given that double quotes are used, since in character class, \ can be used to escape ^, -, ] to disable their special meanings, we need to use \ in character class to describe the plain-\ itself. (I cannot even specify the backslash in comment section with one-char) Feb 26 at 13:40
  • 1
    @NeoZoom.lua I'm afraid your comments are misstransformed by the markdown engine but indeed \ is used to escape special character in the character class (e.g. \]) and if you need \ in the character class you need to double it \\. Feb 26 at 13:53
1

We don't know what you want that pattern to match or why you added those backslashes in the first place. Do you want to include backslashes into the match or is it an attempt to somehow escape the closing bracket?

If you want to include \ in the match, then yes, you must escape it because doing so turns it into a literal \. Otherwise, your \ would escape the ], which would make your pattern invalid:

E401: Pattern delimiter not found: "[a-zA-Z0-9_:\]"

See :help /\]:

- To include a literal ']', '^', '-' or '\' in the collection, put a
  backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".

Copilot is shit.

Whether you are using double quotes or single quotes makes no practical difference in this context because the pattern is not a string expression. Your double quotes could as well be /, @, or whatever. They are just arbitrary delimiters so neither the rules for :help expr-" nor those for :help expr-' apply.

The following commands are all equivalent and valid:

syntax match MyGroup "[a-zA-Z0-9_:\\]"
syntax match MyGroup '[a-zA-Z0-9_:\\]'
syntax match MyGroup /[a-zA-Z0-9_:\\]/
syntax match MyGroup @[a-zA-Z0-9_:\\]@

The following commands are all equivalent and invalid:

syntax match MyGroup "[a-zA-Z0-9_:\]"
syntax match MyGroup '[a-zA-Z0-9_:\]'
syntax match MyGroup /[a-zA-Z0-9_:\]/
syntax match MyGroup @[a-zA-Z0-9_:\]@

See :help syn-pattern.

7
  • 1
    Oh, and fuck Copilot.
    – romainl
    Feb 27 at 9:40
  • 1
    Is Copilot the problem or are people who assume Copilot to have a godlike understanding the problem?
    – Friedrich
    Feb 27 at 10:07
  • 1
    Both. And both have their roots in other older, wider, unsolved problems within the industry. A year ago I would have said "fuck Google" or "fuck SO" for the exact same reasons. FWIW, I actually organized an official Copilot demo at my job a few weeks ago. The demo was very interesting and the devrel who put on the show was open about the thing's use cases and shortcomings. I came out of it with a more nuanced opinion than before (rejection > caution). 1/2
    – romainl
    Feb 27 at 13:03
  • 1
    2/2 My view is that it's good if used as a kind of "smart" snippet system, where you understand the code it inserts in the buffer on your behalf and it's bad if used to produce the logic in your stead. In other words, you ask it for a database connection because you don't want to type it all out, not because you don't know how to write it. A view in line with what I think about completion, etc. I've seen it used as "pilot", not "copilot" and it was scary. Like "I don't know python but thanks to copilot I was able to produce a script that does X and Y". We shouldn't go that way.
    – romainl
    Feb 27 at 13:09
  • 1
    My own experiences pretty much mirror yours. I've heard technical people from GitHub stress that it's copilot and how you need to stay in control. I've seen 150 AI-generated lines of C++ that essentially copy a file (note: that's bad even by C++'s standards). I actually enjoy not having to type out every loop and being able to focus on the interesting problems. On the other hand, you have to scrutinize everything that little cheat puts into your code. In total, I'd say I'm slightly more productive. But the potential to misuse is there - and a real problem.
    – Friedrich
    Feb 27 at 14:31

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.