Wikia

Vim Tips Wiki

Putting the current file on the Windows clipboard

Talk0
1,612pages on
this wiki
Tip 432 Printable Monobook Previous Next

created 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:

command! Copyfile let @*=substitute(expand("%:p"), '/', '\', 'g')

This copies the entire path and filename of the current file (with any slashes replaced with backslashes) onto the clipboard. You can then paste the value wherever needed, such as a File, Open dialog.

CommentsEdit

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 @*=expand('%:p')<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.


To copy current file contents (rather than just the file name) to clipboard:

:%y*

And map it, if you want:

:map <F3> :%y*<CR>

File-content copying 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.


Around Wikia's network

Random Wiki