55

What is the most simple way to send current full file name with file path to clipboard?

What I am using now is messages buffer: I copy file name that appears there after saving a file. But, I suppose, there should be much more simple way.

1

11 Answers 11

53

Why no one tell the simple solution.

Just go to your dired buffer then press 0 w or C-u 0 w.

This will call dired-copy-filename-as-kill which gives you full path of a file. If you want current dir, just delete the file at the end of it or you can use the function below, then bind it to any key you like.

(defun my/dired-copy-dirname-as-kill ()
  "Copy the current directory into the kill ring."
  (interactive)
  (kill-new default-directory))

PS: personally I go to current directory from file buffer using dired-jump

2
  • 4
    In total form: C-x C-j 0 w
    – LionKimbro
    Commented Aug 4, 2020 at 6:29
  • Instead of my/dired-copy-dirname-as-kill, you can also get the path of the directory with C-x C-j C-x C-j C-0 w.
    – Ben
    Commented Oct 27, 2020 at 12:01
33

I use this:

(defun my-put-file-name-on-clipboard ()
  "Put the current file name on the clipboard"
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (with-temp-buffer
        (insert filename)
        (clipboard-kill-region (point-min) (point-max)))
      (message filename))))
1
  • 6
    You can do that without the temp buffer, using "kill-new": (let ((x-select-enable-clipboard t)) (kill-new filename))
    – sanityinc
    Commented Mar 10, 2010 at 22:26
27

In Emacs Prelude I use:

(defun prelude-copy-file-name-to-clipboard ()
  "Copy the current buffer file name to the clipboard."
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (kill-new filename)
      (message "Copied buffer file name '%s' to the clipboard." filename))))
1
  • 2
    The prelude-copy-file-name-to-clipboard command is no longer part of Prelude. As suggested by the commit message for the commit which removed it, consider using the easy-kill package instead.
    – ack
    Commented Apr 19, 2017 at 23:10
23

If you want to write the name/path of the current buffer you can type C-u M-: and then either (buffer-file-name) - for the full path - or (buffer-name) for the buffer name.

That is:

M-: + ellisp expression evaluates an ellisp expression in the mini-buffer

C-u write the output to the current buffer

Does not exactly answer to the question but could be useful if someone use this or other function sporadically, and prefers to not initialize the function at every startup.

2
  • 2
    In order to properly answer the question one could use M-- C-M-k afterwards (kill the last sexp). Guess it is a little bit cumbersome, but could be used as a macro.
    – rsenna
    Commented Sep 16, 2013 at 17:13
  • 4
    M-: (kill-new (buffer-file-name)) gets the full filename/path to clipboard/kill-ring or M-: (kill-new (buffer-name)) just the filename
    – Rob H
    Commented Jul 10, 2019 at 11:53
18

In the Spacemacs distribution, you can press Spacefyy. It brings up a number of options that you can choose to copy (e.g., buffer name, file name, file path with line number, etc.).

The function spacemacs/copy-file-name seems to originate from this blog post: Emacs: Show Buffer File Name.

(defun camdez/show-buffer-file-name ()
  "Show the full path to the current file in the minibuffer."
  (interactive)
  (let ((file-name (buffer-file-name)))
    (if file-name
        (progn
          (message file-name)
          (kill-new file-name))
      (error "Buffer not visiting a file"))))
3
  • The source for this implementation was short and sweet. I would copy/paste it here so if the link changes the answer is still available. I liked this code the best.
    – PatS
    Commented Oct 24, 2018 at 21:15
  • 1
    @PatS Good suggestion. I noticed that the function was originally written by someone else, so I added a courtesy link to that blog post as well. Commented Oct 25, 2018 at 8:16
  • On a newer spacemacs, SPC f y will bring up a number of options; e.g., SPC f y y copies the full path and SPC f y Y copies the path relative to a repository root. Very hand! Commented Feb 11, 2020 at 13:30
6

There's a buffer-extension - and it has copy-buffer-file-name-as-kill function. It even asks You what to copy: name, full name or a directory name.

Edit:

I use modified version of copy-buffer-file-name-as-kill from buffer-extension.el:

(defun copy-buffer-file-name-as-kill (choice)
  "Copyies the buffer {name/mode}, file {name/full path/directory} to the kill-ring."
  (interactive "cCopy (b) buffer name, (m) buffer major mode, (f) full buffer-file path, (d) buffer-file directory, (n) buffer-file basename")
  (let ((new-kill-string)
        (name (if (eq major-mode 'dired-mode)
                  (dired-get-filename)
                (or (buffer-file-name) ""))))
    (cond ((eq choice ?f)
           (setq new-kill-string name))
          ((eq choice ?d)
           (setq new-kill-string (file-name-directory name)))
          ((eq choice ?n)
           (setq new-kill-string (file-name-nondirectory name)))
          ((eq choice ?b)
           (setq new-kill-string (buffer-name)))
          ((eq choice ?m)
           (setq new-kill-string (format "%s" major-mode)))
          (t (message "Quit")))
    (when new-kill-string
      (message "%s copied" new-kill-string)
      (kill-new new-kill-string))))
6

If you use Doom Emacs, it can be done with SPC f y.

4

Lots of good answers here, though I think for the "most simple way", as described in the question, there's room for improvement. Here's what I came up with (with thanks to other answers for the bits and pieces):

M-: (kill-new (buffer-file-name)) RET

This does precisely what you asked for -- takes the filename of the current buffer, and puts it in the "kill ring" and, depending on your settings, also the system clipboard. (See emacswiki/CopyAndPaste for more details on that part.)

If you want to do this regularly, then setting up a function like listed in the other answers, and binding it to an available key sequence, would make it easier to do frequently. But the above works with no prior setup, which I'm interpreting to be more "simple".

1
  • 1
    Like this answer, goes well with C-r in the history, especially if you save it persistently.
    – joao
    Commented Aug 6, 2023 at 23:32
0

To paste the current file path in the buffer, the most simple way I see is to do: C-u M-! pwd (this might not work on Windows systems though).

Alternatively, you can use C-x C-b to show the file paths of all opened buffers.

0

This is what has worked for me on MacOS 10.15.7, GNU Emacs 27.1

(defun copy-current-buffer-file-name ()
  (interactive)
  (shell-command (concat "echo " (buffer-file-name) " | pbcopy")))

set keybinding to "C-x M-f":

(global-set-key (kbd "C-x M-f") 'copy-current-buffer-file-name)

FYI: For a real beginner reading this, you need to add those lines to your init.el file.

0

If you use evil, you automatically have two registers to get the current and the "alternate" file paths (if you switch to another buffer): % and #. You can paste them e.g. with "%p ersp. "#p.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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