0

I was wondering if it's possible to use a Perl RegEx in a .vim syntax file instead of Vim Speak.

If I have a syntax file such as

/usr/share/vim/vim80/syntax/none.vim

" Vim syntax file
" Language:     Custom None
" Maintainer:   Nobody

if exists("b:current_syntax")
  finish
endif

syn match noneIndentError "*Some Vim Speak RegEx*"

hi def link noneIndentError Error

let b:current_syntax = "none"

Is it possible to use a Perl RegEx inside of the syn match instead of a Vim Speak one? My RegEx is written for Perl and I can't seem to get it to work in Vim Speak

(?:^)(?: {4})*+( {1,})

The RegEx just tests for spaces that aren't included in the indented 4 spaces, you can see it here if you'd like

https://regex101.com/r/oBCjY4/1

4
  • What is "Vim Speak"?
    – Kusalananda
    May 7 at 9:49
  • 1
    I don't see anything in there that can't be matched in Vim. Are you having trouble with zero-width matches?
    – muru
    May 7 at 9:50
  • If this can be matched in Vim that would be amazing but I cannot for the life of me figure it out, it isn't happy with the *+ May 7 at 10:37
  • You don't need the *+. It's just an optimization for Perl. A plain old * would work fine.
    – muru
    May 7 at 11:11
2

The following match should do what you want:

syn match noneIndentError /^\( \{4\}\)*\zs \{,3\}/

Using \zs, you can exclude the pattern before it from the match, so it's a simpler way to write zero-width look-arounds (it's similar to \K in Perl). Then it matches up to 3 spaces.

Here's how it matches various lines:

      foo #
   bar
     
    
        baz

  1. 6 spaces followed by text
  2. 3 spaces followed by text
  3. 5 spaces
  4. 4 spaces
  5. 8 spaces followed by text
5
  • This does exactly what I need it to do, I am unfamiliar with \zs but it looks like that was just what it needed May 7 at 13:43
  • Just doing a bit of digging and I'm finding some strange functionality with \zs. It looks like it breaks some other match expressions, for example if you have syn match checkNumber "\<[0-9]\+\>" and then the \zs expression following after May 7 at 15:52
  • @TheLovelySausage can't reproduce in 8.2-2815: i.stack.imgur.com/xZ2PK.png (I did :syn match checkNumber "\<[0-9]\+\>", :hi link checkNumber Keyword, :syn match noneIndentError /^\( \{4\}\)*\zs \{,3\}/, :hi link noneIndentError Error)
    – muru
    May 8 at 4:17
  • @TheLovelySausage Sometimes using \zs and ze in syn match expressions might pose a problem if you expect the part that's not part of the match to actually match some other syn match rule... In those cases, you'll need to do an actual zero-width look-behind or look-ahead. But it's hard to tell you what's wrong with your setup without more details, so perhaps ask a separate question for that if you like. BTW, you might like the Vi and Vim Stack Exchange for questions on Vim (oh, I guess already told you about it in another question...)
    – filbranden
    May 9 at 23:56
  • Hello again @filbranden !, yes it's me again and it looks like you're right, the first instance of \zs is not finding a match so the expression doesn't have a \ze. I onlt really know Perl RegEx so this has me baffled. I def want to try the Vim exchange May 10 at 6:49

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.