From 11a23144ad189b6befee2332383188dbffecee6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 22 Feb 2022 11:18:43 +0100 Subject: [PATCH] feat(copypath): add plugin to copy file paths to clipboard (#7569) Closes #7569 Closes #10714 --- plugins/copypath/README.md | 15 +++++++++++++++ plugins/copypath/copypath.plugin.zsh | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 plugins/copypath/README.md create mode 100644 plugins/copypath/copypath.plugin.zsh diff --git a/plugins/copypath/README.md b/plugins/copypath/README.md new file mode 100644 index 000000000..1e5a463a6 --- /dev/null +++ b/plugins/copypath/README.md @@ -0,0 +1,15 @@ +# copypath plugin + +Copies the path of given directory or file to the system clipboard. + +To use it, add `copypath` to the plugins array in your zshrc file: + +```zsh +plugins=(... copypath) +``` + +## Usage + +- `copypath`: copies the absolute path of the current directory. + +- `copypath `: copies the absolute path of the given file. diff --git a/plugins/copypath/copypath.plugin.zsh b/plugins/copypath/copypath.plugin.zsh new file mode 100644 index 000000000..8fe0a85f4 --- /dev/null +++ b/plugins/copypath/copypath.plugin.zsh @@ -0,0 +1,15 @@ +# Copies the path of given directory or file to the system or X Windows clipboard. +# Copy current directory if no parameter. +function copypath { + # If no argument passed, use current directory + local file="${1:-.}" + + # If argument is not an absolute path, prepend $PWD + [[ $file = /* ]] || file="$PWD/$file" + + # Copy the absolute path without resolving symlinks + # If clipcopy fails, exit the function with an error + print -n "${file:a}" | clipcopy || return 1 + + echo ${(%):-"%B${file:a}%b copied to clipboard."} +}