Jay Taylor's notes

back to listing index

How do I insert the current filename into the contents in Vim?

[web search]
Original source (unix.stackexchange.com)
Tags: vim howto unix.stackexchange.com
Clipped on: 2018-07-17

After some searching, I got to know :echo @% displays the current filename in the bottom line of vim-screen.

I would like to dump the filename (with and without full path) into the contents of the file without leaving vim.

Is there any way to do this?

asked Dec 4 '12 at 14:31
mtk
7,2202559100
up vote 23 down vote accepted

I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.

So, to insert just the file name:

:r! echo %

And, to include the full path:

:r! echo %:p

For more info:

:help read
:help filename-modifiers 
answered Dec 4 '12 at 14:41
jonyamo
1,6331117

If you need to do this frequently, it might be useful to bind a key sequence to what you want, as per http://vim.wikia.com/wiki/Insert_current_filename

inserts the current filename without the extension at the cursor position, when you are in insert mode.

:inoremap \fn <C-R>=expand("%:t:r")<CR>

To keep the extension use:

:inoremap \fn <C-R>=expand("%:t")<CR>

To insert the absolute path of the directory the file is in use:

:inoremap \fn <C-R>=expand("%:p:h")<CR>

To insert the relative path of the directory the file is in use:

:inoremap \fn <C-R>=expand("%:h")<CR>

The above all work directly in vim for the session, or you can put it in the .vimrc (where the leading colon on the line is optional).

answered Oct 20 '17 at 18:36
Randall
1579

A simple way is to run:

!!echo %
  • !! is replacing the current line with the result of the command following it.

  • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.

The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run

!!basename %
answered Dec 4 '12 at 14:40
jlliagre
44.2k578122

As can be seen in :h registers, the "% register contains the current file name. The :pu[t] command inserts the content of a register into the text.

So, to insert the actual filename you can type either of these, in command mode:

:put %

or

"%p

To insert the filename with the full path, type

:put=expand('%:p')

in command mode.


More info:

:h pu[t]

By typing "rp you can paste the contents of register "r.

answered Dec 4 '12 at 21:55
braunmagrin
23115
  • The :put "%:p" doesn't seem to work for me. It seems to work only with expand(), like in: :put =expand('%:p'), which makes it not much less cumbersome than <c-r>=expand('%:p') unfortunately. – akavel Sep 16 '15 at 11:21
  • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose. – braunmagrin Sep 29 '15 at 23:00

The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.

answered Dec 4 '12 at 14:55
Kevin
25.4k95797

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.

asked

5 years, 7 months ago

viewed

6,678 times

active

8 months ago

Hot Network Questions

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.

This site is not affiliated with Linus Torvalds or The Open Group in any way.