Emacs: Copy File Path of Current Buffer 🚀

By Xah Lee. Date: . Last updated: .

Here's a command to copy the file path of current buffer.

(defun xah-copy-file-path (&optional DirPathOnlyQ)
  "Copy current buffer file path or dired path.
Result is full path.
If `universal-argument' is called first, copy only the dir path.

If in dired, copy the current or marked files.

If a buffer is not file and not dired, copy value of `default-directory'.

URL `http://xahlee.info/emacs/emacs/emacs_copy_file_path.html'
Version 2018-06-18 2021-09-30"
  (interactive "P")
  (let (($fpath
         (if (string-equal major-mode 'dired-mode)
               (let (($result (mapconcat 'identity (dired-get-marked-files) "\n")))
                 (if (equal (length $result) 0)
                     (progn default-directory )
                   (progn $result))))
           (if (buffer-file-name)
             (expand-file-name default-directory)))))
     (if DirPathOnlyQ
           (message "Directory copied: %s" (file-name-directory $fpath))
           (file-name-directory $fpath))
         (message "File path copied: %s" $fpath)
         $fpath )))))

Be sure to give it a easy key. [see Emacs: How to Define Keys]

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Xah Emacs Commands