Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Duplicate|296}}
 
 
{{TipImported
 
{{TipImported
 
|id=600
 
|id=600
|previous=599
+
|previous=598
 
|next=601
 
|next=601
|created=October 30, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Igor Keselman
 
|author=Igor Keselman
 
|version=5.7
 
|version=5.7
 
|rating=23/17
 
|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, email attachment, reference in a document, etc).
+
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 two mappings are useful for copying the file name to the Windows clipboard.
+
These mappings are useful for copying the file name to the clipboard.
 
In Vim normal mode, I type <tt>,cs</tt> or <tt>,cl</tt>, then simply paste the name into another document using the regular Windows paste command.
 
*<tt>,cs</tt> copies just the file name.
 
*<tt>,cl</tt> copies the file name including its full path.
 
   
 
<pre>
 
<pre>
  +
" Convert slashes to backslashes for Windows.
nmap ,cs :let @*=expand("%")&lt;CR&gt;
 
 
if has('win32')
nmap ,cl :let @*=expand("%:p")&lt;CR&gt;
 
 
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>
 
</pre>
   
 
This maps the following keys:
==References==
 
 
*<code>,cs</code> copies just the filename.
*{{help|expand()}}
 
 
*<code>,cl</code> copies the filename including it's full path.
*{{help|filename-modifiers}}
 
  +
*<code>,c8</code> copies the filename in 8.3 format for DOS and Windows 9x
*{{help|quotestar}}
 
   
 
You can then simply paste the name into another document using the regular paste command.
==Comments==
 
Under Windows, this will copy the path in 8.3 short format:
 
nmap ,c8 :let @*=expand("%:p:8")&lt;CR&gt;
 
   
  +
===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 />
Another method for the first map:
 
  +
<pre>
nnoremap &lt;leader&gt;cs :let @+=@%&lt;cr&gt;
 
 
nmap ,cs :let @*=expand("%")<CR>
  +
nmap ,cl :let @*=expand("%:p")<CR>
  +
</pre>
   
  +
----
 
  +
with <br />
I use the following in my vimrc:
 
   
 
<pre>
 
<pre>
  +
nmap ,cs :let @+=expand("%")<CR>
" Copy the filename of the current buffer into the clipboard
 
  +
nmap ,cl :let @+=expand("%:p")<CR>
if has('win32')
 
nnoremap &lt;Leader&gt;ff :let @*=substitute(expand("%:p"), "/", "\\", "g")&lt;cr&gt;
 
else
 
nnoremap &lt;Leader&gt;ff :let @*=expand("%:p")
 
endif
 
 
</pre>
 
</pre>
   
This takes care of flipping the slashes for Windows platforms. This came from [[VimTip296]].
 
   
  +
----
 
  +
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==

Revision as of 05:38, 13 July 2012

Tip 600 Printable Monobook Previous Next

created 2003 · complexity basic · author Igor Keselman · version 5.7


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.

" 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

This maps the following keys:

  • ,cs copies just the filename.
  • ,cl copies the filename including it's full path.
  • ,c8 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:

  nmap ,cs :let @*=expand("%")<CR>
  nmap ,cl :let @*=expand("%:p")<CR>


with

  nmap ,cs :let @+=expand("%")<CR>
  nmap ,cl :let @+=expand("%:p")<CR>


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

Comments