Vim Tips Wiki
m (Added to Windows Category + minor enhancements)
(adjust previous/next navigation + minor manual clean)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=432
 
|id=432
  +
|previous=431
|title=Putting the current file on the Windows clipboard
 
  +
|next=434
|created=March 5, 2003 6:39
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Salman Halim
 
|author=Salman Halim
 
|version=6.0
 
|version=6.0
 
|rating=5/4
 
|rating=5/4
  +
|category1=Windows
|text=
 
  +
|category2=
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.
 
 
}}
 
}}
 
Sometimes I want to use the file I'm editing in Vim (on Windows) in another application; I created the following command:
  +
<pre>
 
command! Copyfile let @*=substitute(expand("%:p"), '/', '\', 'g')
  +
</pre>
   
 
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.
== 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:
 
:map &lt;leader&gt;cf :Copyfile&lt;cr&gt;
 
   
 
==Comments==
I noticed that hitting :C&lt;tab&gt; was almost as fast as the mapping characters anyway.
 
 
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:
 
  +
<pre>
salmanhalim--AT--hotmail.com
 
  +
:map <Leader>cf :Copyfile<CR>
, March 5, 2003 6:42
 
  +
</pre>
----
 
Here is my map to do the same thing:
 
nn &lt;silent&gt;&lt;C-G&gt; :let --AT--*='&lt;C-R&gt;=expand("%:p")&lt;CR&gt;'&lt;CR&gt;:f&lt;CR&gt;
 
 
I overloaded &lt;C-G&gt;, and because I set ssl, I don't need worry about backslash.
 
   
 
I noticed that hitting :C<tab> was almost as fast as the mapping characters anyway.
   
maxiangjiang--AT--hotmail.com
 
, March 5, 2003 7:52
 
 
----
 
----
 
Here is my map to do the same thing:
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.
 
  +
<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.
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.
 
 
salmanhalim--AT--hotmail.com
 
, March 6, 2003 6:22
 
 
----
 
----
 
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.
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 &lt;silent&gt;&lt;C-G&gt; :let --AT--*=expand("%:p")&lt;CR&gt;:f&lt;CR&gt;
 
   
 
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.
Assignments accept expressions that aren't literals so you don't need the register expansion.
 
   
salmanhalim--AT--hotmail.com
 
, March 6, 2003 11:04
 
 
----
 
----
  +
To copy current file contents (rather than just the file name) to clipboard:
 
  +
<pre>
Wow. Thanks. That is great!
 
  +
:%y*
I just modified all register assignment statements in my vimrc!
 
  +
</pre>
   
  +
And map it, if you want:
maxiangjiang--AT--hotmail.com
 
  +
<pre>
, March 7, 2003 14:56
 
  +
:map <F3> :%y*<CR>
----
 
  +
</pre>
For vim rookies -like me- the way to copy current file content in Windows clipboard is
 
:1,$ y *
 
   
 
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.
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 &lt;F3&gt; :1,$ y * &lt;CR&gt;
 
   
epoc--AT--bsab.com
 
, March 8, 2003 2:50
 
 
----
 
----
 
simpler one:
 
:%y*&lt;CR&gt;
 
 
maxiangjiang--AT--hotmail.com
 
, March 9, 2003 20:04
 
----
 
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.
 
 
Anon
 
, March 10, 2003 6:02
 
----
 
<!-- parsed by vimtips.py in 0.515946 seconds-->
 
 
[[Category:Windows]]
 

Latest revision as of 10:18, 23 April 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.

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:

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