created March 5, 2003 · complexity basic · author Salman Halim · version 6.0
Sometimes I want to use the file I'm editing in Vim (on Windows) in another application; I created the following command:
com! Copyfile let @*=substitute(expand("%:p"), '/', '\', 'g')
This simply copies the entire path and filename of the current file -- substituting backslashes for slashes (in case there are any) -- onto the Windows clipboard. I can then just go and paste the value wherever I want (such as a File -> Open dialog).
For example, for my _vimrc file, I get c:\vim\_vimrc in the clipboard.
As an addendum, I didn't want to make a mapping out of this because I don't do it often enough; however, one can easily do something like:
:map <Leader>cf :Copyfile<CR>
I noticed that hitting :C<tab> was almost as fast as the mapping characters anyway.
Here is my map to do the same thing:
nn <silent><C-G> :let @*='<C-R>=expand("%:p")<CR>'<CR>:f<CR>
I overloaded <C-G>, and because I set ssl, I don't need worry about backslash.
The problem with your mapping is you're corrupting the clipboard every time you want to simply get information about where one is in the file. (I actually use ctrl-g every so often and it would be nice if it didn't have a side effect.) Basically, for new users, overloading existing commands with side effects can become confusing and not intuitive.
The whole point of 'ssl' is to replace backslashes with FORWARD slashes. It doesn't help with pasting the filepath into another Windows application -- SOME of them may understand forward slashes but many will not, hence the substitution.
You don't need the quotes around the let expression in your mapping.
Let me correct myself: you DO need the quotes around your mapping the way you have it. However, you can simply change it to:
nn <silent><C-G> :let @*=expand("%:p")<CR>:f<CR>
Assignments accept expressions that aren't literals so you don't need the register expansion.
For vim rookies -like me- the way to copy current file content in Windows clipboard is
:1,$ y *
that is, yanking from line 1 to end-of-file -represented by $- into register '*'. After that, go to Notepad and do a simply CTRL + V. And map it, if you want
:map <F3> :1,$ y * <CR>
The file-content copy things probably should be a separate tip altogether. Plus, it's not complete without a mention of the 'clipboard' and 'guioptions' options. Try :help gui-clipboard for more details.