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
ib.
  • 27,830
  • 11
  • 80
  • 100
user847614
  • 351
  • 3
  • 11
  • 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. – sidyll Aug 09 '11 at 18:20
  • 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. – user847614 Aug 09 '11 at 19:14

2 Answers2

6
augroup MyJavaAUGroup
  au! BufRead,BufNewFile,BufEnter *.java,*Test.java,*/test/* <<YOURCOMMANDHERE>>
augroup END
John Kaul
  • 340
  • 1
  • 5
  • This seemed to apply a simple command 3 times when creating a new file with path `code/algos/graphs/test/KruskalTest.java` – user847614 Aug 09 '11 at 19:12
  • 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. – John Kaul Aug 09 '11 at 21:27
  • 1
    @JohnKaul "commas" in "autocmd-pattern" stand for alternative not conjunction, so your pattern is weaker and matches more than is intended. – derenio Mar 21 '13 at 15:12
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.

ib.
  • 27,830
  • 11
  • 80
  • 100
  • 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. – user847614 Aug 10 '11 at 07:07