Search code examples
vimeclimautocmd

autocmd for running Eclim's :JavaImportOrganize


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!


Solution

  • 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.