55

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?

6 Answers 6

61

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

4
  • 3
    Maybe I'm misreading that, but isn't <c-r> mapped as the redo command? Dec 4, 2012 at 15:38
  • 16
    In insert mode, <c-r> specifies that the next character is a register.
    – Kevin
    Dec 4, 2012 at 15:43
  • Righto, thanks. Totally forgot to switch to insert mode... Dec 4, 2012 at 15:49
  • 4
    You can also do "%p in normal or visual modes.
    – naught101
    Aug 25, 2017 at 2:36
41

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 
0
20

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.

2
  • 3
    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, 2015 at 11:21
  • @akavel You're correct, I'll update the answer. Thank you! ;) And, indeed, both are equally verbose. Sep 29, 2015 at 23:00
7

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 %
0
3

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).

2

I was googling a simplest way to insert current file name (without path or extension) to current position (no new line, no replace current line). So combining previous answers and this link, this is what I was looking for.

In insert mode:

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

Then next time (with the command already in history):

<C-R>=<UP>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .