50

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.

Dmytro Kuznetsov
  • 743
  • 1
  • 6
  • 7
  • See https://stackoverflow.com/questions/3669511/the-function-to-show-current-files-full-path-in-mini-buffer – Alan Jan 10 '23 at 23:34

10 Answers10

42

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

azzamsa
  • 1,805
  • 2
  • 20
  • 28
31

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))))
scottfrazer
  • 17,079
  • 4
  • 51
  • 49
  • 6
    You can do that without the temp buffer, using "kill-new": (let ((x-select-enable-clipboard t)) (kill-new filename)) – sanityinc Mar 10 '10 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))))
Ben Quigley
  • 727
  • 4
  • 18
Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • 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`](https://github.com/leoliu/easy-kill) package instead. – ack Apr 19 '17 at 23:10
21

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.

ajeje
  • 590
  • 5
  • 11
  • 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 Sep 16 '13 at 17:13
  • 3
    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 Jul 10 '19 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"))))
xuhdev
  • 8,018
  • 2
  • 41
  • 69
Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
  • 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 Oct 24 '18 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. – Dag Høidahl Oct 25 '18 at 08: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! – Fred Schoen Feb 11 '20 at 13:30
6

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

Cthutu
  • 8,713
  • 7
  • 33
  • 49
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))))
Adobe
  • 12,967
  • 10
  • 85
  • 126
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".

lindes
  • 9,854
  • 3
  • 33
  • 45
  • 1
    Like this answer, goes well with `C-r` in the history, especially if you save it persistently. – joao Aug 06 '23 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.

crocefisso
  • 793
  • 2
  • 14
  • 29
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.