简体   繁体   English

Emacs 中剪贴板的文件路径

[英]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.但是,我想,应该有更简单的方法。

Why no one tell the simple solution.为什么没有人告诉简单的解决方案。

Just go to your dired buffer then press 0 w or Cu 0 w .只需转到您的 dired 缓冲区,然后按0 wCu 0 w

This will call dired-copy-filename-as-kill which gives you full path of a file.这将调用dired-copy-filename-as-kill ,它为您提供文件的完整路径。 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 PS:我个人使用 dired-jump 从文件缓冲区转到当前目录

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

In Emacs Prelude I use:Emacs Prelude 中,我使用:

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

If you want to write the name/path of the current buffer you can type Cu M-: and then either (buffer-file-name) - for the full path - or (buffer-name) for the buffer name.如果您想写入当前缓冲区的名称/路径,您可以键入Cu M-:然后(buffer-file-name) - 表示完整路径 - 或(buffer-name)表示缓冲区名称。

That is:那是:

M-: + ellisp expression evaluates an ellisp expression in the mini-buffer M-: + ellisp expression计算迷你缓冲区中的 ellisp 表达式

Cu write the output to the current buffer Cu将输出写入当前缓冲区

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.不能完全回答这个问题,但如果有人偶尔使用这个或其他函数,并且不想在每次启动时都初始化该函数,可能会很有用。

In the Spacemacs distribution, you can press Space f y y to display the buffer name in the minibuffer and copy it to the kill ring.在 Spacemacs 发行版中,可以按Space f y y显示 minibuffer 中的缓冲区名称,并将其复制到 kill ring。

The function spacemacs/show-and-copy-buffer-filename seems to originate from this blog post:Emacs: Show Buffer File Name .函数spacemacs/show-and-copy-buffer-filename似乎源自这篇博文: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"))))

There's a buffer-extension - and it has copy-buffer-file-name-as-kill function.有一个缓冲区扩展- 它具有copy-buffer-file-name-as-kill功能。 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 :我使用buffer-extension.elcopy-buffer-file-name-as-kill修改版本:

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

To paste the current file path in the buffer, the most simple way I see is to do: Cu M-! pwd要将当前文件路径粘贴到缓冲区中,我看到的最简单的方法是: Cu M-! pwd Cu M-! pwd (this might not work on Windows systems though). Cu M-! pwd (虽然这可能不适用于 Windows 系统)。

Alternatively, you can use Cx Cb to show the file paths of all opened buffers.或者,您可以使用Cx Cb来显示所有打开的缓冲区的文件路径。

This is what has worked for me on MacOS 10.15.7, GNU Emacs 27.1这就是在 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 "Cx Mf":将键绑定设置为“Cx Mf”:

(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.仅供参考:对于阅读本文的真正初学者,您需要将这些行添加到您的init.el文件中。

如果您使用 Doom Emacs,则可以使用SPC fy来完成。

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 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.这正是您所要求的——获取当前缓冲区的文件名,并将其放入“kill ring”,并且根据您的设置,还放入系统剪贴板。 (See emacswiki/CopyAndPaste for more details on that part.) (有关该部分的更多详细信息,请参阅emacswiki/CopyAndPaste 。)

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.如果您想定期执行此操作,那么像其他答案中列出的那样设置一个 function 并将其绑定到可用的键序列,这样可以更容易地经常执行此操作。 But the above works with no prior setup, which I'm interpreting to be more "simple".但是上面的工作没有事先设置,我将其解释为更“简单”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM