How do I insert the current filename into the contents in Vim?
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 "=
.
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
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
.