显示当前文件在迷你缓冲区中的完整路径的功能

发布于 2024-09-18 01:50:58 字数 654 浏览 9 评论 0原文

我需要获取正在使用 emacs 编辑的文件的完整路径。

  • 有这样的功能吗?
  • 如果没有,那么 elisp 函数可以得到什么?
  • 如何将结果(路径名)复制到剪贴板以便我可以重复使用它?

我使用的是 Mac OS X 和 Aqumacs。

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

添加

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

结合我得到的两个答案,我可以得到我想要的。感谢您的回答。还有一些问题。

  • (文件真实名称)有什么用?
  • 我可以将路径名复制到系统(操作系统)的剪贴板,而不是杀环,以便我可以在其他应用程序中使用该信息吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

淡忘如思 2024-09-25 01:51:26

如果你使用evil,你可以使用:reg(ister)来显示evil寄存器。 % 将是当前路径,您也可以使用它粘贴到当前缓冲区(或 # 如果您更改了缓冲区)。

If you use evil, you can use :reg(ister) to show the evil registers. % will be the current path, which you can also use to paste into the current buffer (or # in case you changed buffers).

み青杉依旧 2024-09-25 01:51:24

要按照标题所说的那样(显示迷你缓冲区中的当前文件路径),您可以这样做:

M-x buffer-file-name

要在模式行中永久显示它,您可以使用这个:

(setq-default mode-line-buffer-identification
              (list 'buffer-file-name
                    (propertized-buffer-identification "%12f")
                    (propertized-buffer-identification "%12b")))

或者这个(颜色+缩写):

(setq-default mode-line-buffer-identification
              '((:eval
                 (list (propertize (abbreviate-file-name
                                    (expand-file-name buffer-file-name))
                                   'face 'font-lock-string-face)))))

To do what the title says (show the current file path in the minibuffer) you can do this:

M-x buffer-file-name

To permanently show it in the mode-line, you can use this:

(setq-default mode-line-buffer-identification
              (list 'buffer-file-name
                    (propertized-buffer-identification "%12f")
                    (propertized-buffer-identification "%12b")))

Or this (color + abbrev) :

(setq-default mode-line-buffer-identification
              '((:eval
                 (list (propertize (abbreviate-file-name
                                    (expand-file-name buffer-file-name))
                                   'face 'font-lock-string-face)))))
浪漫之都 2024-09-25 01:51:22

我认为来自 [0] 的 copy-buffer-file-name-as-kill 正是您所需要的。它还可以选择仅复制目录名或仅复制文件名。

[0] http://www.emacswiki.org/emacs/download/buffer-扩展名.el

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

美男兮 2024-09-25 01:51:21

我可以将路径名复制到系统(操作系统)的剪贴板(而不是 Kill Ring),以便我可以在其他应用程序中使用该信息吗?

如果您使用 xclip (Linux)、pbcopy (Mac)、putclip (Cygwin) 之类的工具,就可以实现。

我个人使用包装脚本 cp 分别进行复制和粘贴,首先从标准输入读取,后者写入标准输出。这样,它就适用于我的所有开发平台:

(shell-command (format "echo '%s' | c" buffer-file-name))

我发现这比使用 Emacs 剪贴板支持更可靠且可配置。例如,我的 c 命令将输入​​复制到 Linux 上的所有 3 个剪贴板(主剪贴板、辅助剪贴板),因此我可以使用 Ctrl-V 或中键单击进行粘贴。

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?

You can if you shell out to something like xclip (Linux), pbcopy (Mac), putclip (Cygwin).

I personally use wrapper scripts c and p for copy and paste respectively, the first reading from standard input, the latter writing to standard output. That way, this works on all my development platforms:

(shell-command (format "echo '%s' | c" buffer-file-name))

I find this more reliable and configurable than using the Emacs clipboard support. For example, my c command copies the input to all 3 clipboards on Linux (primary, secondary, clipboard), so I can paste with either Ctrl-V or middle click.

烟酒忠诚 2024-09-25 01:51:20

最简单的方法是

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

向特雷·杰克逊借用
我想出了这个:

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

您可以在网站上找到更多信息

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

So尛奶瓶 2024-09-25 01:51:19

我已经使用以下代码很长时间了。
当我在模式行中的缓冲区名称上按鼠标中键时,它会将完整文件路径复制到终止环。当我在模式行中的缓冲区名称上按 Shift-鼠标-2 时,它仅将缓冲区名称复制到终止环。

(defun copy-buffer-file-name (event &optional bufName)
  "Copy buffer file name to kill ring.
If no file is associated with buffer just get buffer name.
"
  (interactive "eP")
  (save-selected-window
    (message "bufName: %S" bufName)
    (select-window (posn-window (event-start event)))
    (let ((name (or (unless bufName (buffer-file-name)) (buffer-name))))
      (message "Saved file name \"%s\" in killring." name)
      (kill-new name)
      name)))

(define-key mode-line-buffer-identification-keymap [mode-line mouse-2] 'copy-buffer-file-name)
(define-key mode-line-buffer-identification-keymap [mode-line S-mouse-2] '(lambda (e) (interactive "e") (copy-buffer-file-name e 't)))

I have the following code already in use for a long time.
It copies the full file path to the kill ring when I press the middle mouse button on the buffer name in the mode-line. It copies just the buffer name to the kill-ring when I press shift-mouse-2 on the buffer-name in the mode-line.

(defun copy-buffer-file-name (event &optional bufName)
  "Copy buffer file name to kill ring.
If no file is associated with buffer just get buffer name.
"
  (interactive "eP")
  (save-selected-window
    (message "bufName: %S" bufName)
    (select-window (posn-window (event-start event)))
    (let ((name (or (unless bufName (buffer-file-name)) (buffer-name))))
      (message "Saved file name \"%s\" in killring." name)
      (kill-new name)
      name)))

(define-key mode-line-buffer-identification-keymap [mode-line mouse-2] 'copy-buffer-file-name)
(define-key mode-line-buffer-identification-keymap [mode-line S-mouse-2] '(lambda (e) (interactive "e") (copy-buffer-file-name e 't)))
只是一片海 2024-09-25 01:51:18

Cu Cx Cb 列出当前访问文件的缓冲区。

C-u C-x C-b lists buffers currently visiting files.

掩饰不了的爱 2024-09-25 01:51:16

Cx Cd,也可通过Mx list-directory调用,将显示当前文件的目录,您只需按“Enter”键即可清除迷你缓冲区。其他详细信息可在此处获取。

C-x C-d, also callable via M-x list-directory, will show you the directory for your current file, and you only need to hit the "Enter" key to clear the minibuffer. Additional details are available here.

岁月染过的梦 2024-09-25 01:51:15

Cx Cb 显示缓冲区列表以及每个缓冲区的文件路径(如果适用)。

C-x C-b shows a list of buffers and the file path for each buffer where applicable.

空城仅有旧梦在 2024-09-25 01:51:14

不需要额外的功能,只需

M-! pwd

No need for extra function, just

M-! pwd
给妤﹃绝世温柔 2024-09-25 01:51:13

你想要的直接实现是:

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

也就是说,我发现能够获取迷你缓冲区中内容的完整路径非常有用,这就是我使用的:

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

然后如果我想要它在剪贴板中,我只需杀死该行(Ca Ck)即可。但是我们可以在上面的命令中轻松地将 truename 复制到剪贴板,只需将最后一行更改为:

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

新部分是对 'kill-new 的调用,它将字符串放入 Kill 环中。

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.

叫思念不要吵 2024-09-25 01:51:12

我的技巧是执行 Cx Cf 就像打开一个文件一样,它会用当前文件路径预填充 minibuff,Cg 退出。比 M-: buffer-file-name 更快,但比任何其他方法都丑陋得多。

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.

沧桑㈠ 2024-09-25 01:51:10

借用Jérôme Radix的答案,如果你只是想快速查看当前缓冲区的文件路径,你可以这样做M-: buffer-file-name

或者,在缓冲区中的某处输入 (buffer-file-name) 并在右括号上运行 Cx Ce (这适用于任何模式,而不仅仅是 lisp 模式) 。

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

相对绾红妆 2024-09-25 01:51:09

内置函数 buffer-file-name 为您提供文件的完整路径。

最好的办法是让你的 emacs 窗口始终显示你的系统名称和你当前正在编辑的缓冲区的完整路径:

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

你也可以这样做:

(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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文