History Report a problem
Article Edit this page Discussion

Copy parts of filename to clipboard

From Vim Tips Wiki

Jump to: navigation, search
 

Tip 891Previous TipNext Tip

Created: March 7, 2005 Complexity: basic Author: MikeTheGuru Minimum version: 5.7 Karma: 7/7 Imported from: Tip#891


I often need the name and/or directory of the file I'm working on in another Windows application.

Therefore I've added to menu.vim:

:amenu 20.351 &Edit.Copy\ fName :let @*=expand("%:t")<CR>
:amenu 20.352 &Edit.Copy\ fPath :let @*=expand("%:p:h")<CR>
:amenu 20.353 &Edit.Copy\ rName :let @*=expand("%:.")<CR>

This will copy

fName: Only the filename (without path)
fPath: Only the path
rName: path+filename relative to current directory

[edit] Comments

Here is what I use on Windows. Since cmd.exe uses backslashes for file sepatators, I write 'path\file' to clipboard instead of 'path/file'.

:nmap zd :let @*=substitute(expand("%:p"),"/","\\","g")<CR>

"%:p" gives me backslashes.


The slashes you get depend on your setting for 'shellslash'. I have the following:


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

" Copy the current file (with full path) into the clipboard (mapping for Copyfile)
nmap <s-f4> :let @*=substitute(expand("%:p"), '/', '\', 'g')<cr>
" Copy the current file (just the name and extension) into the clipboard
nmap <c-s-f4> :let @*=expand( "%:t" )<cr>
" Split and edit the file that's on the clipboard
nmap <c-f4> :execute "Sp " . @*<cr>

For Java classes:

" Copies the fully qualified (package.class) name of the current Java class or the filename only (no extension) if there
" is no package statement in the class.
function! GetFqn()
  let pos = Mark()
  let packageLine = '^\s*package\s\+\(.*\);'
  0
  let packageLineNumber = search( packageLine, 'W' )
  execute pos
  if ( packageLineNumber == 0 )
    return expand( "%:t:r" )
  endif
  let package = substitute( getline( packageLineNumber ), packageLine, '\1', '' )
  let result = package . "." . expand( "%:t:r" )
  return result
endfunction
com! Copyfqn let @*=GetFqn() | echo @*

The two lines involving Mark and pos are Benji's functions to save and restore the cursor position without using mappings. If you don't have access to them, you could replace them with something like:

" Store the cursor position
normal ma

And, later:

" Restore the cursor position
normal `a

Vim 7 has another flag for the search() function that allows it to not move the cursor, so the entire store/restore cursor position thing might possibly be dispensed with there.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Terminator Wiki
Legend of Zelda Wiki
Flash Gordon
Everquest II Wiki
Yo-Yo Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Copy parts of filename to clipboard"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation