I have a folder with some input files (text files with extention .in).

In same folder, I have made a Java program. I want to test the input of all the input files in this Java program.

In a GUI, I would just copy the text content in the input file and paste it in the console when I run my Java program, but I wonder if there is a really smart way to test the input files in the vim editor.

1 Answer 1


Not really a job for Vim. Use make or a plain bash script to run your tests. You can use Vim's :make integration to run the whole battery from the editor. :help makeprg if you are not writing a Makefile.

Example Makefile:

test: *.in

%.in: FORCE
    java MyProgram $@


Then a simple :make (or :make test, if you defined other rules) will suffice. If you don't want to make another file, you could do something ugly like this:

:set makeprg=bash\ -c\ 'for\ i\ in\ *.in;\ do\ java\ MyProgram\ $i;\ done'      

(remember to escape every space, and triple-escape every vertical bar.) With this, again, :make runs everything.

If you really are asking for a Vim answer, I guess it would be something like

:r!ls *.in
:g/^/execute ":!java MyProgram ".getline(".")
  • If I just want to run one input file at a time, what should I do? If I just want to run input the content of abc.in in my java program?
    – Jamgreen
    Feb 16, 2016 at 9:48
  • 2
    :!java MyProgram abc.in should be good, no?
    – Amadan
    Feb 16, 2016 at 9:50
  • And if you have the input file loaded up in Vim, you can just do :!java MyProgram %. Further, you could change the 'makeprg' for .in files also... :let &makeprg = 'java MyProgram %'... Feb 16, 2016 at 19:22
  • If I'm in the shell and I have the files MyProgram.java and MyProgram.class, I can edit the java file in vim with vim MyProgram.java and run the compiled java program with java MyProgram. But if I write :!java MyProgram abc.in, it doesn't work. I guess I'm messing up what should be written in the console and what should be written in vim.
    – Jamgreen
    Feb 17, 2016 at 1:24
  • Indeed, :!java MyProgram abc.in is a vim command. It is also sensitive to the current directory (check :pwd). There is no "one true answer" here, since all of it is very contextual, and you'd need to tweak according to your project structure and what exactly you are trying to achieve.
    – Amadan
    Feb 17, 2016 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.