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.


I do this:

  1. C-x C-v (find-alternate-file)
  2. C-a (move-beginning-of-line)
  3. C-k (kill-line)
  4. C-g (keyboard-quit)

It's quicker than using the minibuffer history. If all you want is the base name, it's even faster - just skip the C-a in the second step.

  • 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 Zakharyaschev May 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 Ha Sep 18 '13 at 9:45
  • @TruongHa Yeah, the other solution (the answer below) stopped working in Emacs 24 for me.... – imz -- Ivan Zakharyaschev Jan 16 '15 at 12:23
  • @imz--IvanZakharyaschev, replacing <down> with M-n may be more ergonomic. – Brady Trainor May 11 '15 at 17:28

The quickest way to copy the name of the current file in the default setup is

C-x C-f             find-file
down                next-history-element
C-SPC C-a M-w       select and copy the minibuffer contents
C-g                 abort find-file
  • 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 Zakharyaschev Mar 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 Zakharyaschev Mar 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. – Gilles 'SO- stop being evil' May 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 Zakharyaschev May 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. – Gilles 'SO- stop being evil' May 10 '11 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.