Dmytro Kuznetsov
Dmytro Kuznetsov

Reputation: 823

File path to clipboard in Emacs

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.

Upvotes: 58

Views: 22834

Answers (11)

Julian
Julian

Reputation: 1002

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.

Upvotes: 0

Dag Høidahl
Dag Høidahl

Reputation: 8345

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"))))

Upvotes: 18

lindes
lindes

Reputation: 10240

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".

Upvotes: 4

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56595

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))))

Upvotes: 27

Cthutu
Cthutu

Reputation: 8907

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

Upvotes: 6

Anderson Saunders
Anderson Saunders

Reputation: 340

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.

Upvotes: 0

crocefisso
crocefisso

Reputation: 903

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.

Upvotes: 0

azzamsa
azzamsa

Reputation: 2115

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

Upvotes: 57

Adobe
Adobe

Reputation: 13467

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))))

Upvotes: 6

ajeje
ajeje

Reputation: 610

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.

Upvotes: 23

scottfrazer
scottfrazer

Reputation: 17327

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))))

Upvotes: 33

Related Questions