clipboard.zsh 2.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# System clipboard integration
#
# This file has support for doing system clipboard copy and paste operations
# from the command line in a generic cross-platform fashion.
#
# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
# "system clipboard", and the X Windows `xclip` command must be installed.

# clipcopy - Copy data to clipboard
#
# Usage:
#
#  <command> | clipcopy    - copies stdin to clipboard
#
#  clipcopy <file>         - copies a file's contents to clipboard
#
function clipcopy() {
  emulate -L zsh
  local file=$1
  if [[ $OSTYPE == darwin* ]]; then
    if [[ -z $file ]]; then
      pbcopy
    else
      cat $file | pbcopy
    fi
  elif [[ $OSTYPE == cygwin* ]]; then
    if [[ -z $file ]]; then
      cat > /dev/clipboard
    else
      cat $file > /dev/clipboard
    fi
  else
34
    if (( $+commands[xclip] )); then
Andrew Janke's avatar
Andrew Janke committed
35
36
37
38
39
      if [[ -z $file ]]; then
        xclip -in -selection clipboard
      else
        xclip -in -selection clipboard $file
      fi
40
    elif (( $+commands[xsel] )); then
Andrew Janke's avatar
Andrew Janke committed
41
42
43
44
45
      if [[ -z $file ]]; then
        xsel --clipboard --input 
      else
        cat "$file" | xsel --clipboard --input
      fi
46
    else
Andrew Janke's avatar
Andrew Janke committed
47
48
      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
      return 1
49
50
51
52
53
54
55
56
57
58
    fi
  fi
}

# clippaste - "Paste" data from clipboard to stdout
#
# Usage:
#
#   clippaste   - writes clipboard's contents to stdout
#
Andrew Janke's avatar
Andrew Janke committed
59
60
61
62
63
64
65
66
67
68
69
#   clippaste | <command>    - pastes contents and pipes it to another process
#
#   clippaste > <file>      - paste contents to a file
#
# Examples:
#
#   # Pipe to another process
#   clippaste | grep foo
#
#   # Paste to a file
#   clippaste > file.txt
70
71
72
73
74
75
76
function clippaste() {
  emulate -L zsh
  if [[ $OSTYPE == darwin* ]]; then
    pbpaste
  elif [[ $OSTYPE == cygwin* ]]; then
    cat /dev/clipboard
  else
77
    if (( $+commands[xclip] )); then
Andrew Janke's avatar
Andrew Janke committed
78
      xclip -out -selection clipboard
79
    elif (( $+commands[xsel] )); then
Andrew Janke's avatar
Andrew Janke committed
80
81
82
      xsel --clipboard --output
    else
      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
83
84
85
      return 1
    fi
  fi
Andrew Janke's avatar
Andrew Janke committed
86
}