Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert filename without the path in vim

Tags:

vim

filenames

I knew that in the insert mode i can insert the filiename with the path using CTRL-R-%.

But i'd like to insert only the filename without the path part. Is there a similar command for that?

like image 422
schurik Avatar asked Aug 19 '13 09:08

schurik


People also ask

How to add a filename in Vim?

In insert mode, type Ctrl-R then % to insert the name of the current file. 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.

How to show file name in Vim?

Save this answer. Show activity on this post. ctrl + g will do it.

How to get full path of file in Vim?

Note, %F will be the full path. To get a path relative to the working directory, use %f .

How do I insert a last line in vi?

In normal mode press A (uppercase). Vim will be switched to insert mode, and the cursor will be moved to the end of the current line ready for input. IMO, this should be the accepted answer.


2 Answers

You can use

<C-r>=expand("%:t")<CR>

See :help filename-modifiers.

Edit

<C-r> is used in insert mode to insert the content of a register. "% is the register that contains the name of the current file.

"= is the expression register, it contains the result of the expression that comes after =:

<C-r>=2+27+6<CR>        --> 35
<C-r>=expand("%:t")<CR> --> file.txt
like image 178
romainl Avatar answered Oct 11 '22 22:10

romainl


While in insert mode, you can use

CTRL-R =expand('%:t')
like image 43
Lieven Keersmaekers Avatar answered Oct 11 '22 23:10

Lieven Keersmaekers