views:

1502

answers:

3

Assuming the current buffer is a file open for edit, so :e does not display E32: No file name.

I would like to yank one or all of:

  • The file name exactly as show on the status line, e.g. ~\myfile.txt
  • A full path to the file, e.g. c:\foo\bar\myfile.txt
  • Just the file name, e.g. myfile.txt
+14  A: 

Almost what you're asking for, and it might do: Ctrl+R % pulls the current filename into where you are (command prompt, edit buffer, ...). See this Vim Tip for more.

dwc
when you exit edit mode you can add the file name: ctrl-" ctrl-% p
stefanB
stefanB: I can't get that to work :(
dwc
`Ctrl+R %` works while in `insert` mode -- but in `edit` mode, `Ctrl+R` means redo.
Kevin Panko
What stefanB meant was "%p for normal/command mode. That's the same as <C-r>% in insert/cmdline-mode.
graywh
+8  A: 

Try this:

:let @" = expand("%")

this will copy the file name to the unamed register, then you can use good old 'p' to paste it. and of course you can map this to a key for quicker use.

:nmap cp :let @" = expand("%")

you can also use this for full path

:let @" = expand("%:p")

use :help expand for more details

JD
Wow, exactly as desired, thank you very much!
Dave Tapley
+1  A: 

Combining information from a couple of other answers: If you want to yank the current full path to a file and put it into the command buffer in another window, first do :let @" = expand("%:p"), then move to another window and type Ctrl+R ".

Useful for copying a file while staying in the same directory and keeping the old one open. For example:

Start: Editing src/com/benatkin/paint/shapes/Circle.java

  1. Type :let @" = expand("%:p") (The path gets yanked to the main clipboard buffer.)

  2. Open a new window with :sp

  3. Type :e Ctrl+R"

  4. Use the arrow keys to go back to Circle and change it to Square, and press <CR>

End: Editing src/com/benatkin/paint/shapes/Square.java

Ben Atkin