Yank file name / path of current buffer in Vim
Assuming the current buffer is a file open for edit, so :e
does not display E32: No file name
.
I would like to yank one or all of:
- The file name exactly as show on the status line, e.g.
~\myfile.txt
- A full path to the file, e.g.
c:\foo\bar\myfile.txt
- Just the file name, e.g.
myfile.txt
Solution 1:
TL;DR
:let @" = expand("%")
>
this will copy the file name to the unamed register, then you can use good old p
to paste it. and of course you can map this to a key for quicker use.
:nmap cp :let @" = expand("%")<cr>
you can also use this for full path
:let @" = expand("%:p")
Explanation
Vim uses the unnamed register to store text that has been deleted or copied (yanked), likewise when you paste it reads the text from this register.
Using let
we can manually store text in the register using :let @" = "text"
but we can also store the result of an expression.
In the above example we use the function expand
which expands wildcards and keywords. in our example we use expand('%')
to expand the current file name. We can modify it as expand('%:p')
for the full file name.
See :help let
:help expand
:help registers
for details
Solution 2:
Almost what you're asking for, and it might do: Ctrl+R %
pulls the current filename into where you are (command prompt, edit buffer, ...). See this Vim Tip for more.
Solution 3:
If you want to put the current buffer filename in your system-level clipboard, try changing the register to @+:
" relative path
:let @+ = expand("%")
" full path
:let @+ = expand("%:p")
" just filename
:let @+ = expand("%:t")
Edit 20140421:
I commonly use these, so I created some shortcuts. Linux Vims apparently operate slightly differently than Mac Vims, so there is a special case for that as well. If you put the following in your ~/.vimrc
:
" copy current file name (relative/absolute) to system clipboard
if has("mac") || has("gui_macvim") || has("gui_mac")
" relative path (src/foo.txt)
nnoremap <leader>cf :let @*=expand("%")<CR>
" absolute path (/something/src/foo.txt)
nnoremap <leader>cF :let @*=expand("%:p")<CR>
" filename (foo.txt)
nnoremap <leader>ct :let @*=expand("%:t")<CR>
" directory name (/something/src)
nnoremap <leader>ch :let @*=expand("%:p:h")<CR>
endif
" copy current file name (relative/absolute) to system clipboard (Linux version)
if has("gui_gtk") || has("gui_gtk2") || has("gui_gnome") || has("unix")
" relative path (src/foo.txt)
nnoremap <leader>cf :let @+=expand("%")<CR>
" absolute path (/something/src/foo.txt)
nnoremap <leader>cF :let @+=expand("%:p")<CR>
" filename (foo.txt)
nnoremap <leader>ct :let @+=expand("%:t")<CR>
" directory name (/something/src)
nnoremap <leader>ch :let @+=expand("%:p:h")<CR>
endif
Then for example <leader>cf
will copy the relative path of the current buffer (the default leader is backslash (\
)). I often use these for running commands on a file or doing other things on the command line. I don't really use the last filename / directory name often.
You might consider more intuitive mappings like <leader>cfr
for relative, <leader>cfa
for absolute, <leader>cff
for just filename, <leader>cfd
for directory.