File path to clipboard in Emacs

前端 未结 7 941
闹比i
闹比i 2021-01-31 02:40

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

相关标签:
7条回答
  • 2021-01-31 03:04

    In Emacs Prelude I use:

    (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))))
    
    0 讨论(0)
  • 2021-01-31 03:07

    There's a buffer-extension - and it has copy-buffer-file-name-as-kill function. 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:

    (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))))
    
    0 讨论(0)
  • 2021-01-31 03:09

    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))))
    
    0 讨论(0)
  • 2021-01-31 03:09

    In the Spacemacs distribution, you can press Spacefyy to display the buffer name in the minibuffer and copy it to the kill ring.

    The function spacemacs/show-and-copy-buffer-filename seems to originate from this blog post: 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"))))
    
    0 讨论(0)
  • 2021-01-31 03:13

    Why no one tell the simple solution.

    Just go to your dired buffer then press 0 w or C-u 0 w.

    This will call dired-copy-filename-as-kill which gives you full path of a file. 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

    0 讨论(0)
  • 2021-01-31 03:15

    To paste the current file path in the buffer, the most simple way I see is to do: C-u M-! pwd (this might not work on Windows systems though).

    Alternatively, you can use C-x C-b to show the file paths of all opened buffers.

    0 讨论(0)
提交回复
热议问题