Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yank file name / path of current buffer in Vim

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
like image 640
davetapley Avatar asked May 27 '09 16:05

davetapley


People also ask

How do I see the filename in Vim?

If all that is wanted is to display the name of the current file, type Ctrl-G (or press 1 then Ctrl-G for the full path). When using @% , the name is displayed relative to the current directory. In insert mode, type Ctrl-R then % to insert the name of the current file.

How do I get full path of file in Gvim?

Pressing 1 followed by Ctrl + G shows the full path of the current file.


1 Answers

TL;DR

: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("%")<cr>

you can also use this for full path

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

Explanation

Vim uses the unnamed register to store text that has been deleted or copied (yanked), likewise when you paste it reads the text from this register.

Using let we can manually store text in the register using :let @" = "text" but we can also store the result of an expression.

In the above example we use the function expand which expands wildcards and keywords. in our example we use expand('%') to expand the current file name. We can modify it as expand('%:p') for the full file name.

See :help let :help expand :help registers for details

like image 94
JD Frias Avatar answered Sep 28 '22 07:09

JD Frias