-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.zsh
172 lines (151 loc) · 4.01 KB
/
functions.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# _____ _ _
# | ___| _ _ __ ___| |_(_) ___ _ __ ___
# | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
# | _|| |_| | | | | (__| |_| | (_) | | | \__ \
# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
# --------------
# PLUGIN MANAGER
# --------------
function zsh_add_file() {
# Function to source files if they exist
[ -f "$ZDOTDIR/$1" ] && source "$ZDOTDIR/$1"
}
function zsh_add_plugin() {
PLUGIN_NAME=$(echo $1 | cut -d "/" -f 2)
if [ -d "$ZDOTDIR/plugins/$PLUGIN_NAME" ]; then
# For plugins
zsh_add_file "plugins/$PLUGIN_NAME/$PLUGIN_NAME.plugin.zsh" || \
zsh_add_file "plugins/$PLUGIN_NAME/$PLUGIN_NAME.zsh"
else
git clone "https://github.com/$1.git" "$ZDOTDIR/plugins/$PLUGIN_NAME"
fi
}
function zsh_add_completion() {
PLUGIN_NAME=$(echo $1 | cut -d "/" -f 2)
if [ -d "$ZDOTDIR/plugins/$PLUGIN_NAME" ]; then
# For completions
completion_file_path=$(ls $ZDOTDIR/plugins/$PLUGIN_NAME/_*)
fpath+="$(dirname "${completion_file_path}")"
zsh_add_file "plugins/$PLUGIN_NAME/$PLUGIN_NAME.plugin.zsh"
else
git clone "https://github.com/$1.git" "$ZDOTDIR/plugins/$PLUGIN_NAME"
fpath+=$(ls $ZDOTDIR/plugins/$PLUGIN_NAME/_*)
[ -f $ZDOTDIR/.zccompdump ] && $ZDOTDIR/.zccompdump
fi
completion_file="$(basename "${completion_file_path}")"
if [ "$2" = true ] && compinit "${completion_file:1}"
}
# -------------------
# CUSTOM ZSH COMMANDS
# -------------------
function oj() {
if [ $# -eq 0 ]; then
jobs -d
else
fg %$1
fi
}
function kj ()
{
kill %$1
}
function take() {
mkdir -p -- "$1" &&
cd -P -- "$1"
echo $(pwd) | tr "\n" " " | clip.exe
CLIP=$(pwd)
}
function ii() {
FILE=$@
CHECK="${FILE##*.}"
if [[ ! -f $1 ]] then # Create the file if it doesn't exist
touch $1
fi
# Add custom defaults
if [ "$CHECK" = "md" ]; then
nvim $FILE
elif [ "$CHECK" = "pdf" ]; then
SumatraPDF.exe $FILE
elif [ "$CHECK" = "py" ]; then
nvim $FILE
elif [ "$CHECK" = "txt" ]; then
nvim $FILE
# Else use system defaults
else
powershell.exe -c "ii $FILE"
fi
}
function tp() {
if [[ $1 ]]; then
open $1 -a Typora.app
else
open . -a Typora.app
fi
}
function ppy() {
powershell.exe -c "python3 $1"
}
function ccont() {
if [ -f "$1" ]; then
CLIP=$(cat $1)
echo $CLIP | tr "\n" " " | clip.exe
else
echo "File does not exist"
fi
}
# FIXME: So this works on linux
function cpath() {
# use pbcopy if on mac and xclip if on linux
# if on macos, set the command to pbcopy elese set it to xclip
[[ $OSTYPE == "darwin"* ]] && alias copy='pbcopy' || alias copy='xclip -selection clipboard'
# if OS is macos, then copy the path of the current directory
if [[ $# -eq 0 ]]; then
echo $(pwd) | tr "\n" " " | pbcopy
else
if [[ -f $1 ]]; then
echo $(pwd)/$1 | tr "\n" " " | pbcopy
elif [[ -d $1 ]]; then
echo $(pwd)/$1 | tr "\n" "/" | pbcopy
fi
fi
}
function create_latex_homework()
{
if [[ -z $1 ]]; then
cp ~/.dotfiles/.local/share/template.tex .
else
cp ~/.dotfiles/.local/share/template.tex $1
fi
}
function trash ()
{
if [[ -z $@ ]]; then
cd $HOME/.Trash/
else
mv $@ $HOME/.Trash/
fi
}
function what-the-shell ()
{
source $HOME/.dotfiles/github-cli/lazy-load.sh
copilot_what-the-shell $@
}
function git-assist ()
{
source $HOME/.dotfiles/github-cli/lazy-load.sh
copilot_git-assist $@
}
function cd ()
{
source /usr/bin/cd "$1" && exa
}
function gh-assist ()
{
source $HOME/.dotfiles/github-cli/lazy-load.sh
copilot_what-the-shell $@
}
function docker_start ()
{
sudo ln -s ~/Library/Containers/com.docker.docker/Data/docker.raw.sock /var/run/docker.sock
DOCKER_HOST=unix:///var/run/docker.sock docker ps # test that it works using linked socket file
}