Wikia

Vim Tips Wiki

Changes: Putting the current file on the Windows clipboard

Edit

Back to page

(Remove html character entities)
(adjust previous/next navigation + minor manual clean)
 
Line 3: Line 3:
 
|id=432
 
|id=432
 
|previous=431
 
|previous=431
|next=433
+
|next=434
|created=March 5, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Salman Halim
 
|author=Salman Halim
Line 13: Line 13:
 
}}
 
}}
 
Sometimes I want to use the file I'm editing in Vim (on Windows) in another application; I created the following command:
 
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')
+
<pre>
  +
command! Copyfile let @*=substitute(expand("%:p"), '/', '\', 'g')
  +
</pre>
   
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).
+
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.
 
For example, for my _vimrc file, I get c:\vim\_vimrc in the clipboard.
 
   
 
==Comments==
 
==Comments==
 
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:
 
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>
+
<pre>
  +
:map <Leader>cf :Copyfile<CR>
  +
</pre>
   
 
I noticed that hitting :C<tab> was almost as fast as the mapping characters anyway.
 
I noticed that hitting :C<tab> was almost as fast as the mapping characters anyway.
Line 25: Line 25:
 
----
 
----
 
Here is my map to do the same thing:
 
Here is my map to do the same thing:
nn <silent><C-G> :let @*='<C-R>=expand("%:p")<CR>'<CR>:f<CR>
+
<pre>
  +
nn <silent><C-G> :let @*=expand('%:p')<CR>:f<CR>
  +
</pre>
   
 
I overloaded <C-G>, and because I set ssl, I don't need worry about backslash.
 
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 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.
 
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:
+
To copy current file contents (rather than just the file name) to clipboard:
nn <silent><C-G> :let @*=expand("%:p")<CR>:f<CR>
+
<pre>
  +
:%y*
  +
</pre>
   
Assignments accept expressions that aren't literals so you don't need the register expansion.
+
And map it, if you want:
  +
<pre>
  +
:map <F3> :%y*<CR>
  +
</pre>
   
----
+
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.
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>
 
 
----
 
simpler one:
 
:%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.
 
   
 
----
 
----

Latest revision as of 10:18, April 23, 2011

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