Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
FANDOM
Explore
Current Wiki
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Copy filename to clipboard
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Copy filename to clipboard
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=600 |previous=598 |next=601 |created=2003 |complexity=basic |author=Igor Keselman |version=5.7 |rating=23/17 |category1=File Handling |category2= }} Sometimes I need to use the name of the file that I'm editing in another application (compiler, e-mail attachment, reference in a document, etc). These mappings are useful for copying the file name to the clipboard. <pre> " Convert slashes to backslashes for Windows. if has('win32') nmap ,cs :let @*=substitute(expand("%"), "/", "\\", "g")<CR> nmap ,cl :let @*=substitute(expand("%:p"), "/", "\\", "g")<CR> " This will copy the path in 8.3 short format, for DOS and Windows 9x nmap ,c8 :let @*=substitute(expand("%:p:8"), "/", "\\", "g")<CR> else nmap ,cs :let @*=expand("%")<CR> nmap ,cl :let @*=expand("%:p")<CR> endif </pre> This maps the following keys: *<code>,cs</code> copies just the filename. *<code>,cl</code> copies the filename including its full path. *<code>,c8</code> copies the filename in 8.3 format for DOS and Windows 9x You can then simply paste the name into another document using the regular paste command. ===Copying to the Gnome Clipboard=== Under linux, the script above will copy the file path or filename to X Server clipboard (accessed by pressing the middle mouse button). To copy text to the Gnome Clipboard instead replace the following lines:<br /> <pre> nmap ,cs :let @*=expand("%")<CR> nmap ,cl :let @*=expand("%:p")<CR> </pre> with <br /> <pre> nmap ,cs :let @+=expand("%")<CR> nmap ,cl :let @+=expand("%:p")<CR> </pre> This uses the + register instead of the * register. Note: this may work with KDE and XCFE as well, I simply haven't tested (yet). ==References== *{{help|expand()}} *{{help|filename-modifiers}} *{{help|quotestar}} *{{help|'clipboard'}} ==Comments== I find it useful to have mappings both for copying the (full) path of the current file to the system clipboard and to the Vim unnamed register, as well as to copy the short name to the unnamed register: <pre> nnor ,cf :let @*=expand("%:p")<CR> " Mnemonic: Copy File path nnor ,yf :let @"=expand("%:p")<CR> " Mnemonic: Yank File path nnor ,fn :let @"=expand("%")<CR> " Mnemonic: yank File Name </pre> I've never felt the need to copy the short file name to the clipboard, since in most cases the full path will work the same or better. The short file name (without the path) needs :t expand("%:t")
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab