-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
258 lines (238 loc) · 7.57 KB
/
functions
File metadata and controls
258 lines (238 loc) · 7.57 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Kill Process on Port
# -----------------------------------------------------------------------------
function kp() {
# Kill process running on port $1.
if [ -z "$1" ]; then
echo "Usage: kp <port>"
return 1
fi
local pid
pid=$(sudo lsof -t -i:"$1")
if [ -n "$pid" ]; then
sudo kill -9 "$pid"
else
echo "No process found running on port $1"
fi
}
# -----------------------------------------------------------------------------
# Get Process on Port
# -----------------------------------------------------------------------------
function gp() {
# Display process running on port $1.
if [ -z "$1" ]; then
echo "Usage: gp <port>"
return 1
fi
sudo lsof -t -i:"$1"
}
# -----------------------------------------------------------------------------
# Make Directory and Enter It
# -----------------------------------------------------------------------------
function mkd() {
# Create directory $1 (with parents if necessary) and cd into it.
if [ -z "$1" ]; then
echo "Usage: mkd <directory>"
return 1
fi
local dir="$1"
mkdir -p "$dir" && cd "$dir"
}
# -----------------------------------------------------------------------------
# File Size Summary (du)
# -----------------------------------------------------------------------------
function fs() {
# Show sizes for files in the given directories or current dir if none provided.
local arg
if du -b /dev/null > /dev/null 2>&1; then
arg="-sbh"
else
arg="-sh"
fi
if [ "$#" -gt 0 ]; then
du $arg -- "$@"
else
du $arg .[^.]* ./*
fi
}
# -----------------------------------------------------------------------------
# Find & Replace in Files
# -----------------------------------------------------------------------------
function fr() {
# Recursively search files in a given path and replace $2 with $3.
# Usage: fr <path> <search> <replace>
if [ "$#" -lt 3 ]; then
echo "Usage: fr <path> <search> <replace>"
return 1
fi
local path="$1" search="$2" replace="$3"
if command -v gsed &>/dev/null; then
find "$path" -type f -exec gsed -i "s/${search}/${replace}/g" {} +
else
if [[ "$(uname)" == "Darwin" ]]; then
# macOS sed needs an empty string for in‑place edits.
find "$path" -type f -exec sed -i '' "s/${search}/${replace}/g" {} +
else
find "$path" -type f -exec sed -i "s/${search}/${replace}/g" {} +
fi
fi
}
# -----------------------------------------------------------------------------
# Git Diff with Color & Word Highlights
# -----------------------------------------------------------------------------
function diff() {
# Use git diff with --no-index and colorized word differences.
# Note: This overrides the built-in diff command.
git diff --no-index --color-word "$@"
}
# -----------------------------------------------------------------------------
# Execute Command in All Tmux Panes
# -----------------------------------------------------------------------------
function ta() {
# Send the specified command (arguments) to every tmux pane.
if ! command -v tmux &>/dev/null; then
echo "tmux is not installed."
return 1
fi
local pane
for pane in $(tmux list-panes -F '#P'); do
for arg in "$@"; do
tmux send-keys -t "$pane" "$arg" Space
done
tmux send-key -t "$pane" Enter
done
}
# -----------------------------------------------------------------------------
# Zettelkasten Entry Shortcut
# -----------------------------------------------------------------------------
function z() {
# Create a new zettelkasten entry file in $HOME/zet/<YYYYMMDD>/README.adoc.
local d base_dir
d=$(date +'%Y%m%d')
base_dir="$HOME/zet/$d"
dinit "$base_dir/README.adoc"
}
# -----------------------------------------------------------------------------
# Document Initialization
# -----------------------------------------------------------------------------
function dinit() {
# Initialize a document if it doesn't exist.
# Usage: dinit <filepath>
if [ -z "$1" ]; then
echo "Usage: dinit <filepath>"
return 1
fi
local file dir
file="$1"
dir=$(dirname "$file")
if [ ! -f "$file" ]; then
mkdir -p "$dir"
cat <<'EOF' > "$file"
:toc: macro
:toc-title:
:toclevels: 9
#
toc::[]
EOF
fi
nvim "$file"
}
# -----------------------------------------------------------------------------
# Scratch File Creator
# -----------------------------------------------------------------------------
function scratch() {
# Create and open a scratch file with the given extension in a random /tmp folder.
# Usage: scratch <extension>
if [ -z "$1" ]; then
echo "Usage: scratch <extension>"
return 1
fi
local rnd dir
rnd=$(head /dev/urandom | LC_ALL=C tr -dc 'a-z' | fold -w 8 | head -n 1)
dir="/tmp/$rnd"
mkdir -p "$dir"
cd "$dir" || return 1
nvim "scratch.$1"
}
# -----------------------------------------------------------------------------
# Git Branch Checkout from Origin
# -----------------------------------------------------------------------------
function checkout() {
# Fetch and checkout the specified branch from origin.
# Usage: checkout <branch>
if [ -z "$1" ]; then
echo "Usage: checkout <branch>"
return 1
fi
git fetch
git checkout "$1" "origin/$1"
}
# -----------------------------------------------------------------------------
# Dotnet Test Filter
# -----------------------------------------------------------------------------
function dt() {
# Run dotnet test filtered by FullyQualifiedName.
# Usage: dt <filter>
if [ -z "$1" ]; then
echo "Usage: dt <test_filter>"
return 1
fi
dotnet test --filter="FullyQualifiedName~$1"
}
# -----------------------------------------------------------------------------
# Dotnet Watch Test Filter
# -----------------------------------------------------------------------------
function dtw() {
# Run dotnet watch test filtered by FullyQualifiedName.
# Usage: dtw <test_filter>
if [ -z "$1" ]; then
echo "Usage: dtw <test_filter>"
return 1
fi
dotnet watch test --filter="FullyQualifiedName~$1"
}
# -----------------------------------------------------------------------------
# Dotnet Test with Detailed Console Logging
# -----------------------------------------------------------------------------
function nettest() {
# Run dotnet test with detailed console logging.
# Usage: nettest <test_filter>
if [ -z "$1" ]; then
echo "Usage: nettest <test_filter>"
return 1
fi
dotnet test -l "console;verbosity=detailed" --filter="FullyQualifiedName~$1"
}
# -----------------------------------------------------------------------------
# Adjust Screen Brightness via xrandr
# -----------------------------------------------------------------------------
function bright() {
# Adjust the brightness for the primary display.
# Usage: bright <brightness_value>
if [ -z "$1" ]; then
echo "Usage: bright <brightness_value>"
return 1
fi
if ! command -v xrandr &>/dev/null; then
echo "xrandr not found."
return 1
fi
local output
output=$(xrandr --current | grep " connected" | awk '{print $1}')
xrandr --output "$output" --brightness "$1"
}
# -----------------------------------------------------------------------------
# Main Branch Rebuild Shortcut
# -----------------------------------------------------------------------------
function main() {
# Rebuild the local main branch from origin/main.
if ! command -v git &>/dev/null; then
echo "git is not installed."
return 1
fi
git checkout origin/main &&
git pull origin main &&
git branch -D main &&
git checkout -b main
}