Get the file name without file extension in a Vim function

Vim

Vim Problem Overview


I want to get the file name without the file extension in Vim.

I wrote the following function in my .vimrc file to compile and run the Java program:

:function! JAVA_RUN()
:!javac %^M
:endfunction

map <F3> :execute JAVA_RUN()<CR> :source $HOME/.vimrc<CR>

How can I get the file name without the extension inside the function?

Vim Solutions


Solution 1 - Vim

:help expand() should give you the answer, see http://vimdoc.sourceforge.net/htmldoc/eval.html#expand()">expand()</a>;.

You should use the r modifier for %, with %:r instead of % to get the file name without extension.

If you want to write functions to build and execute files, you should also have a look at the documentation for http://vimdoc.sourceforge.net/htmldoc/eval.html#shellescape()">`shellescape`</a>;, in order to prevent problems with spaces in file name or path.

Solution 2 - Vim

If you want to expand a filename (other than % etc) take a look at fnamemodify()

fnamemodify({fname}, {mods})				*fnamemodify()*
		Modify file name {fname} according to {mods}.  {mods} is a
		string of characters like it is used for file names on the
		command line.  See |filename-modifiers|.

fnamemodify("main.java", ":r") returns main.

Solution 3 - Vim

I literally just read a similar question to this (in that someone else seemed to be trying to configure vim to build automagically for them with the F-key), and wrote an answer about how you can leverage the power of vim's :make command without even needing to write a Makefile. In your case, it's less directly related to the question, but I thought I'd mention it in case you were interested.

Furthermore, someone seems to have written something on Vim Tips Wiki about how to set up vim's :make command to specifically work with Java projects built with ant. I haven't worked with Java in a while myself, but in your case specifically it might be a good place to get started.

Solution 4 - Vim

I came here looking for an answer for a similar question. I wanted to be able to extract the current class name from the java file being edited. I found a very neat way to do this in vim with an abbreviation:

ab xclass <C-R>=expand('%:t:r')<CR>

Place this line in your .vimrc (or similar) for this to work. An abbreviation will auto-trigger as soon as you press space, and so I usually prefix them with 'x' to avoid their accidental expansion.

The trick here is the combination of :t and :r in the argument to expand(). % is the "current file name", :t selects just the tail of the path ("last path component only") and :r selects just the root ("one extension removed"). (quoted parts are from the official expand() documentation.)

So when you are creating a new class in file /a/b/ClassIAmAboutToCreate.java you would type:

public class xclass {

the moment you press space after "xclass", the abbreviation will be expanded to public class ClassIAmAboutToCreate , which is exactly what you need.

Also, note that an abbreviation can be triggered by pressing Ctrl+] which avoids inserting a space after the class name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionungalnanbanView Question on Stackoverflow
Solution 1 - VimtonioView Answer on Stackoverflow
Solution 2 - VimlaktakView Answer on Stackoverflow
Solution 3 - VimAndreView Answer on Stackoverflow
Solution 4 - VimPeter PerháčView Answer on Stackoverflow