5

If I'm coding in cpp, in Vim I can do:

!g++ % && ./a.out

to quickly compile and run the code.

However, if I'm coding in Java, in Vim I can do:

!javac %

for a quick compilation, but for running the java class, I cannot do:

!java %

because I need to put the class name only (without the trailing .java suffix)

Is there a quick way in VIM to do what I did when I was coding in Cpp?

Thanks a lot.

7
  • Why would you want to do this? Just about no useful java programs fit into one class. Commented Feb 23, 2013 at 3:02
  • 3
    !java %:r may work Commented Feb 23, 2013 at 3:04
  • @bmargulies I want to quickly compile&test my java program.. Usually I made it quite small like to be fitting in 1 file Commented Feb 23, 2013 at 3:05
  • @rbernabe what does %:r means? and it works, thx ~:) Commented Feb 23, 2013 at 3:05
  • 1
    Use Eclipse or IntelliJ, not vim, if you want to develop java quickly. Commented Feb 23, 2013 at 3:06

1 Answer 1

8

Vim has modifiers with which you can manipulate filenames. For your use case, the :r modifier returns the root, i.e. the file name with the last extension removed:

:!java %:r

See the full list at :help filename-modifiers; they also can be combined. Note that for more complex builds, a Java build solution like Ant or Maven is probably more effective; these can also be launched from Vim; even integrated into its :make command.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.