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

に公開
2026/01/26

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

脚注
  1. This is the same as the behavior of cat. ↩︎

GitHubで編集を提案

Discussion