Jay Taylor's notesback to listing index
How do I insert the current filename into the contents in Vim?[web search]
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
Is there any way to do this?
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
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).
A simple way is to run:
!!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
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:
To insert the filename with the full path, type
in command mode.
"rp you can paste the contents of register
: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
Maybe I'm misreading that, but isn't
<c-r>mapped as the redo command? – jonyamo Dec 4 '12 at 15:38
10In 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
You can also do
"%pin normal or visual modes. – naught101 Aug 25 '17 at 2:36
Not the answer you're looking for? Browse other questions tagged / vim or ask your own question.
5 years, 7 months ago
Hot Network Questions
Were World War II scrap drives in the United States truly necessary for the procurement of raw materials?
Get numbers matching a pattern from the output of ls?
Can we cite/use publications from other disciplines?
Exact Partial Sum of Harmonic Series
Can someone cease to be a "founder"?
Is this orange-yellowish-flowered plant a wildflower or a weed?
Einstein would be proud
How do ultra-high-frame-rate cameras write so fast?
Can an illusion give a player partial cover?
White chili and chocolate
Is it true that a soda can or a bag of chips will pop in a plane at high altitude?
How to ask my local baker if he is always selling freshly baked bread
Voltameter in TeX?
How to tell a classmate I don’t want to give him a contact for an internship?
Multiclassed character with all classes - any reason this can't be done in 12 levels?
Is there a mathematical basis for the idea that this interpretation of confidence intervals is incorrect, or is it just frequentist philosophy?
International Hunger Crisis
Array-syntax vs pointer-syntax and code generation?
Order of the natural numbers
How can I try to get my coworkers to stop their jokes on LGBTQ+ people
In WW2, why did Germany sink allied supply convoys instead of capturing them?
What is the name of the writing that appears on screen when watching television?
UX in the wild vs UX observed
site design / logo © 2018 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2018.7.16.31036
This site is not affiliated with Linus Torvalds or The Open Group in any way.