Bind SPC f Y to yank path relative to project

Also, both commands now announce "Copied path to clipboard: ..." to make
it clear it worked (and what is being copied).
This commit is contained in:
Henrik Lissner 2021-02-15 14:54:13 -05:00
parent 5e8a668311
commit be9b3ff352
2 changed files with 16 additions and 4 deletions

View file

@ -403,7 +403,8 @@
:desc "Save file as..." "S" #'write-file :desc "Save file as..." "S" #'write-file
:desc "Sudo find file" "u" #'doom/sudo-find-file :desc "Sudo find file" "u" #'doom/sudo-find-file
:desc "Sudo this file" "U" #'doom/sudo-this-file :desc "Sudo this file" "U" #'doom/sudo-this-file
:desc "Yank filename" "y" #'+default/yank-buffer-filename) :desc "Yank file path" "y" #'+default/yank-buffer-path
:desc "Yank file path from project" "Y" #'+default/yank-buffer-path-relative-to-project)
;;; <leader> g --- git/version control ;;; <leader> g --- git/version control
(:prefix-map ("g" . "git") (:prefix-map ("g" . "git")

View file

@ -32,13 +32,24 @@
((error "No kill-ring search backend available. Enable ivy or helm!"))))) ((error "No kill-ring search backend available. Enable ivy or helm!")))))
;;;###autoload ;;;###autoload
(defun +default/yank-buffer-filename () (defun +default/yank-buffer-path (&optional root)
"Copy the current buffer's path to the kill ring." "Copy the current buffer's path to the kill ring."
(interactive) (interactive)
(if-let (filename (or buffer-file-name (bound-and-true-p list-buffers-directory))) (if-let (filename (or (buffer-file-name (buffer-base-buffer))
(message (kill-new (abbreviate-file-name filename))) (bound-and-true-p list-buffers-directory)))
(message "Copied path to clipboard: %s"
(kill-new (abbreviate-file-name
(if root
(file-relative-name filename root)
filename))))
(error "Couldn't find filename in current buffer"))) (error "Couldn't find filename in current buffer")))
;;;###autoload
(defun +default/yank-buffer-path-relative-to-project ()
"Copy the current buffer's path to the kill ring."
(interactive)
(+default/yank-buffer-path (doom-project-root)))
;;;###autoload ;;;###autoload
(defun +default/insert-file-path (arg) (defun +default/insert-file-path (arg)
"Insert the file name (absolute path if prefix ARG). "Insert the file name (absolute path if prefix ARG).