The autocmd pattern to match filename and part of path in Vim
Asked Answered
A

2

9

I am trying to make an auto-cmd triggered by opening a new file with path including test/ and with filename ending in Test.java. Reading in :h autocmd-patterns I can not figure out if either only limited patterns can be used as a file pattern in an auto-cmd or if I am simply doing something wrong with my pattern. The following works for matching any file ending in .java

autocmd! BufNewFile *.java
 \ "command implemented !

Now I try to match new files with a path containing /test/ and with a file name ending in Test.java through the following and derivatives of it

autocmd! BufNewFile */test/*Test.java
 \ "command implemented !

How do I trigger the autocmd on all files having part of their path the test/ folder and the filename ending in Test.java. For instance it should be triggered on doing

$ vim code/algos/graphs/test/KruskalTest.java
Avid answered 9/8, 2011 at 17:13 Comment(2)
It's correct, and should work. Works here. Check if the problem isn't within the command you're passing, or try simply echo "yep" instead.Waynant
Thanks for confirming. When I just used the pattern *.java the command triggered correctly. Changing the pattern to */test/*Test.java` made it not get triggered even with an appropriate path. Will look more into VimScript before continuing.Avid
C
6
augroup MyJavaAUGroup
  au! BufRead,BufNewFile,BufEnter *.java,*Test.java,*/test/* <<YOURCOMMANDHERE>>
augroup END
Celom answered 9/8, 2011 at 18:27 Comment(3)
This seemed to apply a simple command 3 times when creating a new file with path code/algos/graphs/test/KruskalTest.javaAvid
I count 6 conditions that should be met by just opening the file example you just gave with my augroup above. However, please dont misunderstand the usefullness of my example. Please look at the different Buf conditions and also look at the comma seperating the file conditions.Celom
@JohnKaul "commas" in "autocmd-pattern" stand for alternative not conjunction, so your pattern is weaker and matches more than is intended.Magdaleno
C
1

Try using the **/test/*Test.java pattern instead. Also, ensure that you chose the correct auto-command event: For the BufNewFile event to be triggered, it is necessary for the file with a given name not to exist.

Crosshead answered 10/8, 2011 at 1:6 Comment(1)
Thanks. I will try this and report back when I work out what I screwed up in either my .vimrc or ftplugin/java.vim while trying to solve this.Avid

© 2022 - 2024 — McMap. All rights reserved.