0

I am trying to set up an autocmd for running the Eclim command :JavaImportOrganize and :syntax on each time I save a java file.

au BufWritePost {*.java} silent :JavaImportOrganize :syntax on

Which (expectedly) produces an error. I have tried to find an example of an autocmd containing both a Plugin command (in this case loaded from a daemon?) and a normal command but I can't seem to figure out the correct syntax.

Any help much appreciated!

Sebastian N
  • 759
  • 7
  • 13

1 Answers1

2

It would be useful to know what the exact error message is. On my machine, the error is:

Error detected while processing BufWritePost Auto commands for "{*.java}":
E488: Trailing characters: silent :JavaImportOrganize :syntax on

I assume it's the same to you, but it would help a lot to paste it in the question, so people have an easier time guessing what the problem is.

In this case, it's a simple case of combining two commands in a single invocation. You can do this with a | character (see :help :| for details):

:silent JavaImportOrganize | syntax on

Now, in my experiments, this didn't do the trick, since the | syntax on may be considered part of the JavaImportOrganize call. So I had to use exe (:help :execute for more information):

:silent exe 'JavaImportOrganize' | syntax on

The full invocation looks like this:

 au BufWritePost *.java silent exe 'JavaImportOrganize' | syntax on

The curly braces around *.java are not necessary (in fact, I didn't even know they worked :)). The : signs before the commands are not necessary -- they're used in command-line mode, but they're completely optional in scripts.

Andrew Radev
  • 3,982
  • 22
  • 35
  • Thanks! Sorry about not mentioning the error. I have noticed one problem with using silent: it suppresses the "Press Enter" message, but at the same time it also removes the dialog that pops up when there is more than one library to choose from when importing. Is there any way to make the command only suppress the "Press Enter" but **not** the library selection dialog? Thanks. – Sebastian N Feb 26 '15 at 12:10
  • Well, if the "Press Enter" message is due to long output, you can try `:set shortmess+=T` (see `:help avoid-hit-enter`), but that's a global setting. Other than that, I can't think of anything. – Andrew Radev Feb 27 '15 at 08:45