Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 11 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 
share|improve this answer

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 "=.

share|improve this answer
Maybe I'm misreading that, but isn't <c-r> mapped as the redo command? – jonyamo Dec 4 '12 at 15:38
In insert mode, <c-r> specifies that the next character is a register. – Kevin Dec 4 '12 at 15:43
Righto, thanks. Totally forgot to switch to insert mode... – jonyamo Dec 4 '12 at 15:49

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 %



To insert the filename with the full path, type


in command mode.

More info:

:h pu[t]

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

share|improve this answer
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

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 %
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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