views:

56

answers:

2

I need to get the full path of the file that I'm editing with emacs.

  • Is there a function for that?
  • If not, what would be the elisp function for getting that?
  • How can I copy the result (path name) to a clipboard so that I can reuse it?

I'm using Mac OS X and Aqumacs.

(setq filepath (get-fullpath-current-file)) ???
(copy-to-clipboard 'filepath) ???

ADDED

(defun show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name))
  (kill-new (file-truename buffer-file-name))
)
(global-set-key "\C-cz" 'show-file-name)

Combining the two answers that I got, I could get what I want. Thanks for the answers. And some more questions.

  • What's for (file-truename)?
  • Can I copy the path name to System(OS)'s clipboard, not the kill ring so that I can use the info with the other apps?
+1  A: 

The direct implementation of what you want is:

(defun copy-full-path-to-kill-ring ()
  "copy buffer's full path to kill ring"
  (interactive)
  (when buffer-file-name
    (kill-new (file-truename buffer-file-name))))

That said, I find it incredibly useful to be able to get the full path of what is in the minibuffer, and this is what I use:

(define-key minibuffer-local-completion-map "\C-r" 'resolve-sym-link)
(defun resolve-sym-link ()
  "Try to resolve symbolic links into true paths."
  (interactive)
  (beginning-of-line)
  (let* ((file (buffer-substring (point)
                                 (save-excursion (end-of-line) (point))))
         (file-dir (file-name-directory file))
         (file-true-dir (file-truename file-dir))
         (file-name (file-name-nondirectory file)))
    (delete-region (point) (save-excursion (end-of-line) (point)))
    (insert (concat file-true-dir file-name))))

And then if I want it in the clipboard, I just kill the line (C-a C-k). But we could easily copy the truename to the clipboard in the above command, just change the last line to be:

(insert (kill-new (concat file-true-dir file-name)))))

The new part is the call to 'kill-new which puts the string in the kill ring.

Trey Jackson
+2  A: 

It's the built-in function buffer-file-name that gives you the full path of your file.

The best thing to do is to have your emacs window to always show your system-name and the full path of the buffer your currently editing :

(setq frame-title-format
      (list (format "%s %%S: %%j " (system-name))
        '(buffer-file-name "%f" (dired-directory dired-directory "%b"))))

You can also do something like this :

(defun show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name)))

(global-set-key [C-f1] 'show-file-name) ; Or any other key you want
Jérôme Radix