I'm trying to use autocmd BufNewFile to like so:

autocmd BufNewFile  \(.*_spec\)\@!.rb  0r ~/vim/skeleton.rb  
autocmd BufNewFile  *_spec.rb          0r ~/vim/skeleton_spec.rb

So, I want the first line to match all .rb files NOT ending with _spec.rb, and the second line to only match *_spec.rb files. I've tried various incarnations of the first line to no avail.

Thanks in advance.


You should use a look-behind assertion:

autocmd BufNewFile  *\(_spec\)\@<!.rb  0r ~/vim/skeleton.rb
autocmd BufNewFile  *_spec.rb          0r ~/vim/skeleton_spec.rb

The glob pattern *\(_spec\)\@<!.rb is converted to regular expression \(_spec\)\@<!\.rb$ which breaks down as follows:

             \.rb       match literal ".rb"
                 $      this must be the end of the filename
\(_spec\)\@<!           provided "_spec" does not appear right before the dot

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.