Austin Berke
Austin Berke

Reputation: 87

How can I refer to the currently open file in Vim?

In vim, how can I refer to the file that I am currently editing? I want to be able to map a key to run :!java on the file that I am currently working on.

Upvotes: 3

Views: 245

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172768

You can use % to refer to the current buffer's filename; modifiers (cp. :help filename-modifiers) like :p can create a full absolute path, or shave off an extension.

Note that a solution with !java will only work with simple classes in the default package, the use of which is not recommended. Unless you want this just for quick prototyping and throwaway stuff, I'd suggest you use a build system like Ant or Maven, and define a target like run there. Then, with the proper 'makeprg' set in Vim, you can launch your application (also when not directly editing the Java file containing the main class) via

:make run

Upvotes: 6

Related Questions