-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshcp.plugin.zsh
141 lines (121 loc) · 3.82 KB
/
zshcp.plugin.zsh
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# copy the active line from the command line buffer
# onto the system clipboard
function cpbuffer() {
if builtin which clipcopy &>/dev/null; then
printf "%s" "$BUFFER" | clipcopy
else
zle -M "clipcopy not found. Please make sure you have Oh My Zsh installed correctly."
fi
}
zle -N cpbuffer
bindkey -M emacs "^Y" cpbuffer
bindkey -M viins "^Y" cpbuffer
bindkey -M vicmd "^Y" cpbuffer
function cpfile() {
emulate -L zsh
if [[ -z "$1" ]]; then
echo "Usage: cpfile <file>"
return 1
fi
if [[ ! -f "$1" ]]; then
echo "Error: File $1 not found"
return 1
fi
cat "$1" | clipcopy
echo "File content copied to clipboard"
}
# Paste clipboard content to file
function pastefile() {
emulate -L zsh
if [[ -z "$1" ]]; then
echo "Usage: pastefile <filename>"
return 1
fi
if [[ -f "$1" ]]; then
echo -n "File '$1' already exists. Overwrite? (y/N) "
read response
echo
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
return 1
fi
fi
clippaste > "$1" 2>/dev/null
if [[ $? -eq 0 ]]; then
echo "Clipboard contents saved to '$1'"
else
echo "Error: Failed to write clipboard contents to file"
return 1
fi
}
# Copy folder path for later use
function cpfolder() {
emulate -L zsh
if [[ ! -d "${1:-.}" ]]; then
echo "Error: Directory $1 not found"
return 1
fi
folder_path=$(realpath "${1:-.}")
echo "$folder_path" > "${TMPDIR:-/tmp}/.cp_folder_path"
echo "Folder path stored for copying: $folder_path"
}
# Paste previously copied folder
function pastefolder() {
emulate -L zsh
if [[ ! -f "${TMPDIR:-/tmp}/.cp_folder_path" ]]; then
echo "No folder has been copied. Use cpfolder first."
return 1
fi
source_path=$(cat "${TMPDIR:-/tmp}/.cp_folder_path")
if [[ ! -d "$source_path" ]]; then
echo "Error: Source folder no longer exists: $source_path"
rm -f "${TMPDIR:-/tmp}/.cp_folder_path"
return 1
fi
# Get the base name of the source folder
folder_name=$(basename "$source_path")
# If destination is specified, use it; otherwise use the folder name
dest_path="${1:-$folder_name}"
# Check if destination already exists
if [[ -e "$dest_path" ]]; then
echo -n "Destination '$dest_path' already exists. Overwrite? (y/N) "
read response
echo
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
return 1
fi
fi
cp -r "$source_path" "$dest_path"
if [[ $? -eq 0 ]]; then
echo "Copied folder to $dest_path"
else
echo "Error copying folder"
return 1
fi
}
# Copy the current working directory path to clipboard
function cppwd() {
emulate -L zsh
print -n "$PWD" | clipcopy
echo "${(%):-"%B$PWD%b copied to clipboard."}"
}
# Copy command history to clipboard
function cphistory() {
emulate -L zsh
fc -ln -50 1 | clipcopy
echo "Last 50 commands copied to clipboard."
}
# Help function
function cphelp() {
echo "Copy Plugin Commands:"
echo " Ctrl + Y - Copy(Yank) buffer to clipboard"
echo " cpfile <file> - Copy file content to clipboard"
echo " pastefile <file> - Create/overwrite file with clipboard contents"
echo " cpfolder ., <folder> - Copy specified folder, if not specified will copy in pwd"
echo " pastefolder ., <folder> - Create/overwrite copied folder, if not specified will paste in pwd"
echo " cppwd - Copy current directory path"
echo " cphistory - Copy last 50 commands from history"
echo " Ctrl + V - Paste clipboard contents.
echo " cphelp - Show this help message"
}