iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📋
Adding a Cross-Platform Clipboard Copy Command to zsh/fish
zsh
clip.zsh
clip() {
local input
if [[ $# -eq 0 ]]; then
input=$(cat)
else
input=$(cat "$1")
fi
if [[ -n "$IS_WSL" || -n "$WSL_DISTRO_NAME" ]]; then
echo -n "$input" | clip.exe
elif [[ "$(uname)" = "Linux" ]]; then
echo -n "$input" | xsel -bi
elif [[ "$(uname)" = "Darwin" ]]; then
echo -n "$input" | pbcopy
fi
}
fish
clip.fish
function clip
if [ -n "$IS_WSL" ] || [ -n "$WSL_DISTRO_NAME" ]
cat $argv[1] | clip.exe
else if [ "$(uname)" = "Linux" ]
cat $argv[1] | xsel -bi
else if [ "$(uname)" = "Darwin" ]
cat $argv[1] | pbcopy
end
end
Usage Examples
clip a.txt
This copies the contents of a.txt to the clipboard.
Naturally, it also supports input from standard input:
clip
It will accept input from standard input[1].
References
- How to check if a program is run in Bash on Ubuntu on Windows and not just plain Ubuntu?
- How to copy to the clipboard in WSL without using xsel, etc.
- Accessing the clipboard using the xsel command
- Copying standard output to the clipboard in the terminal
-
This is the same as the behavior of cat. ↩︎
Discussion