Jay Taylor's notes
back to listing indexHow 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 vim
.
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:
!!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 %
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
.
-
2The
: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 "=
.
-
-
10
-
-
Your Answer
Not the answer you're looking for? Browse other questions tagged / vim or ask your own question.
asked |
5 years, 7 months ago |
viewed |
6,678 times |
active |
Related
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
-
Battery labeling
-
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.