The function to show current file's full path in mini buffer

前端 未结 12 453
醉梦人生
醉梦人生 2020-12-12 11:58

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 getti
相关标签:
12条回答
  • 2020-12-12 12:19

    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.

    0 讨论(0)
  • 2020-12-12 12:19

    copy-buffer-file-name-as-kill from [0] does exactly what you need I think. It also has the option to copy just directory name, or just file name.

    [0] http://www.emacswiki.org/emacs/download/buffer-extension.el

    0 讨论(0)
  • 2020-12-12 12:20

    The simplest way and would be

    (buffer-name)<(C-x)(C-e)> for the file name to appear in the echo area
    
    (buffer-name)<(C-u)(C-x)(C-e)> would print the location <here>
    

    Borrowing from Trey Jackson I came up with this:

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

    You can find more information on site

    0 讨论(0)
  • 2020-12-12 12:24

    To borrow from Jérôme Radix's answer, if you just want to quickly see the file path of the current buffer, you can do M-: buffer-file-name.

    Alternately, type (buffer-file-name) in the buffer somewhere and run C-x C-e on the closing parenthesis (this will work in any mode, not just lisp-mode).

    0 讨论(0)
  • 2020-12-12 12:25

    My trick is to do a C-x C-f like to open a file, it wil prefill the minibuff with the current file path, C-g to quit. Faster than M-: buffer-file-name but far far uglier than any other methods.

    0 讨论(0)
  • 2020-12-12 12:28

    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 you're 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
    
    0 讨论(0)
提交回复
热议问题