Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the file name without file extension in a Vim function

Tags:

vim

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?

like image 321
ungalnanban Avatar asked Apr 09 '10 07:04

ungalnanban


People also ask

How to get the file name in Vim?

In command mode (after typing a colon), type Ctrl-R then % to insert the name of the current file. The inserted name can then be edited to create a similar name. In normal mode, type "%p to put the name of the current file after the cursor (or "%P to insert the name before the cursor).

How do you get the name of a file without the extension?

GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.

How do I get full path of file in Gvim?

Pressing 1 followed by Ctrl + G shows the full path of the current file.


1 Answers

:help expand() should give you the answer, see expand().

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 shellescape, in order to prevent problems with spaces in file name or path.

like image 190
tonio Avatar answered Sep 27 '22 18:09

tonio