11a23144ad189b6befee2332383188dbffecee6c

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(copypath): add plugin to copy file paths to clipboard (#7569)

Closes #7569
Closes #10714

Diff

 1diff --git a/plugins/copypath/README.md b/plugins/copypath/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..1e5a463a696453b34a6ff28548ccf427a5bb5112
 4--- /dev/null
 5+++ b/plugins/copypath/README.md
 6@@ -0,0 +1,15 @@
 7+# copypath plugin
 8+
 9+Copies the path of given directory or file to the system clipboard.
10+
11+To use it, add `copypath` to the plugins array in your zshrc file:
12+
13+```zsh
14+plugins=(... copypath)
15+```
16+
17+## Usage
18+
19+- `copypath`: copies the absolute path of the current directory.
20+
21+- `copypath <file_or_directory>`: copies the absolute path of the given file.
22diff --git a/plugins/copypath/copypath.plugin.zsh b/plugins/copypath/copypath.plugin.zsh
23new file mode 100644
24index 0000000000000000000000000000000000000000..8fe0a85f412f7db11f39b6d33ed43e0c171172a6
25--- /dev/null
26+++ b/plugins/copypath/copypath.plugin.zsh
27@@ -0,0 +1,15 @@
28+# Copies the path of given directory or file to the system or X Windows clipboard.
29+# Copy current directory if no parameter.
30+function copypath {
31+  # If no argument passed, use current directory
32+  local file="${1:-.}"
33+
34+  # If argument is not an absolute path, prepend $PWD
35+  [[ $file = /* ]] || file="$PWD/$file"
36+
37+  # Copy the absolute path without resolving symlinks
38+  # If clipcopy fails, exit the function with an error
39+  print -n "${file:a}" | clipcopy || return 1
40+
41+  echo ${(%):-"%B${file:a}%b copied to clipboard."}
42+}