It happens quite often that I want to use the path of the file opened in a certain buffer in Emacs (either the full path or the basename) in another place (a buffer or a different X program, say, a terminal). I wonder whether there is some pre-defined subsystem in the Emacs "user interface" that would copy the filename of the current buffer ((buffer-file-name)) to the kill-ring.

Related things: There is a simple command in emacs-w3m that does an analoguos thing (y -- w3m-print-current-url): it prints the URL and copies it to the kill-ring.

Of course, I could simply define the command I want, but I'm asking this question because I hope to learn some user interface subsystem of Emacs that includes such a possibility among other features. (Perhaps, some buffer and path manipulation interfaces.) So that I will know more useful features of Emacs.

Thank you! I've checked that find-alternate-file in my Emacs works indeed this way, but since I have setup it to use ffap and C-x C-v is bound to ffap-alternate-file, this doesn't work for me. (Pressing down in the prompt of my C-x C-v also brings up the current file name.)
–
imz -- Ivan ZakharyaschevMay 10 '11 at 19:34

1

Awesome, it works on my emacs 24! It would be great if someone wrap s these things in a command.
–
Truong HaSep 18 '13 at 9:45

@TruongHa Yeah, the other solution (the answer below) stopped working in Emacs 24 for me....
–
imz -- Ivan ZakharyaschevJan 16 at 12:23

Great! I have learned something indeed, as I hoped. Before, I had no idea that pressing "down" in the minibuffer (when selecting a file) would bring the current buffer's filename into the field.
–
imz -- Ivan ZakharyaschevMar 20 '11 at 16:57

I should have read the documentation for find-file more carefully: "... the visited file name is available through the minibuffer history: type M-n to pull it into the minibuffer."!
–
imz -- Ivan ZakharyaschevMar 20 '11 at 17:44

1

Teddy's solution is much nicer than mine. I didn't know C-x C-v pre-filled the file name, and C-k is a lot quicker to kill the whole line (I should have thought of that). @imz: please accept Teddy's answer, and I'll delete mine.
–
GillesMay 10 '11 at 15:00

2

Actually, I don't like the idea of deleting an answer (the yours one or whatever different answer I'll get here in the future) if it works correctly, because it teaches us (at least, me) something new about the features of Emacs. I think this is much more important than just getting one quickest way to "grab" the filename. Please don't delete your answer!
–
imz -- Ivan ZakharyaschevMay 10 '11 at 19:37

@imz: Ok, then please accept his answer (for people who just want the answer to your question, it should be displayed first) and I'll leave mine up.
–
GillesMay 10 '11 at 19:40