5

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.

imz -- Ivan Zakharyaschev
  • 15,113
  • 15
  • 61
  • 123

2 Answers2

9

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.

Teddy
  • 1,545
  • 10
  • 13
2

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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 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](http://unix.stackexchange.com/q/12939#12939) 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
  • Hmm, this stopped working in Emacs 24 for C-x C-f! The accepted answer (with C-x C-v) still works. I wonder why they have removed this feature of `find-file`.... – imz -- Ivan Zakharyaschev Jan 16 '15 at 12:22
  • @imz--IvanZakharyaschev Which version removed this? It does work in 24.3. – Gilles 'SO- stop being evil' Jan 16 '15 at 13:21
  • @Gilles--Yes, you are correct. On another machine of mine, Emacs 24.3 has this feature. I haven't customized these things. So probably that's because some of the standard elisp libraries are not present on that machine where this doesn't work... My distro (ALT) has split all Emacs into several packages. I'll have a look now... – imz -- Ivan Zakharyaschev Jan 17 '15 at 13:40
  • @Gilles--The problem where C-x C-f DOWN doesn't work is reported in `*Messages*`: byte-code: Cannot open load file: auth-source – imz -- Ivan Zakharyaschev Jan 17 '15 at 13:42
  • @Gilles--And `/usr/share/emacs/24.3/lisp/gnus/auth-source.elc` is from Gnus, which was put into a separate package. No idea why C-x C-f DOWN needs something from Gnus. – imz -- Ivan Zakharyaschev Jan 17 '15 at 13:50
  • @imz--IvanZakharyaschev It doesn't. You must have something hooking into or replacing `find-file`. – Gilles 'SO- stop being evil' Jan 17 '15 at 13:52