-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_aliases
184 lines (165 loc) · 5.86 KB
/
.bash_aliases
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
##############
# Alias file #
##############
# macOS vs Linux specific aliases
if [[ $(uname -a) == "Darwin"* ]]; then
alias ls='ls -alhG'
else
alias ls='ls -alh --color=always'
fi
###################
# General aliases #
###################
alias df='df -h' # Readable sizes
alias du='du -h' # Readable sizes
alias fuck='sudo $(history -p \!\!)' # Retry last command via sudo
alias grep='grep -i --color=auto' # Enable color and case insensitivity
alias less='less --incsearch --use-color'
alias mkdir='mkdir -p' # Make dir and all intermediary dirs
alias rg='rg -i' # Enable case insensitivity
alias scp='scp -Cpr' # Compress, preserve file metadata, and
# copy recursively.
alias sudo='sudo ' # Allows aliased commands to carry over
# when sudoing.
alias vi='v' # The one true god
alias vim='v'
alias weather='curl http://wttr.in/Seattle?FQnu' # Terminal weather. More options
# via /:help request.
#############
# Functions #
#############
# Compress files into various formats
c() {
if [ "$#" -lt 2 ]; then
echo "Usage: c <file/directory> [<file/directory> ...] <target.tar.[gz|bz2|xz|lz|Z|...]> or <target.zip>"
return 1
fi
local target="${!#}" # Last argument is the target
local src=("${@:1:$#-1}") # All arguments except the last one
# Verify that all source files/directories exist
for item in "${src[@]}"; do
if [ ! -e "$item" ]; then
echo "Error: '$item' does not exist."
return 1
fi
done
case "$target" in
*.tar.*|*.tar)
if command -v tar >/dev/null 2>&1; then
if tar -caf "$target" "${src[@]}"; then
echo "Created archive '$target'."
else
echo "Error: Failed to create archive."
return 1
fi
else
echo "Error: 'tar' is not installed."
return 1
fi ;;
*.zip)
if command -v zip >/dev/null 2>&1; then
if zip -r "$target" "${src[@]}"; then
echo "Created archive '$target'."
else
echo "Error: Failed to create archive."
return 1
fi
else
echo "Error: 'zip' is not installed."
return 1
fi ;;
*)
echo "Unsupported compression format: '$target'"
echo "Supported formats are: .tar, .tar.gz, .tar.bz2, .tar.xz, .tar.lz, .tar.Z, .zip"
return 1 ;;
esac
}
# Extract/decompress common archive formats.
x() {
if [ -z "$1" ]; then
echo "Usage: x <archive-file>"
return 1
fi
local archive="$1"
if [ ! -f "$archive" ]; then
echo "Error: '$archive' is not a valid archive file to extract."
return 1
fi
case "$archive" in
*.tar.*|*.tar)
if command -v tar >/dev/null 2>&1; then
if tar -xaf "$archive"; then
echo "Extracted '$archive'."
else
echo "Error: Extraction failed."
return 1
fi
else
echo "Error: 'tar' is not installed."
return 1
fi ;;
*.zip)
if command -v unzip >/dev/null 2>&1; then
if unzip "$archive"; then
echo "Extracted '$archive'."
else
echo "Error: Extraction failed."
return 1
fi
else
echo "Error: 'unzip' is not installed."
return 1
fi ;;
*)
echo "Error: Unsupported archive format."
echo "Supported formats are: .tar, .tar.gz, .tar.bz2, .tar.xz, .tar.lz, .tar.Z, .zip"
return 1 ;;
esac
}
# ls after every cd.
cd() {
builtin cd "$@" && ls;
}
# Search for files matching particular filenames using fzf and ripgrep.
f() {
if [ "$#" -eq 0 ]; then
echo "Need a string to search for!"
return 1
fi
rg --files-with-matches --hidden --no-ignore --no-messages "$1" | \
fzf --query="$1" \
--preview "highlight -O ansi -l {} 2> /dev/null | \
rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' {} || \
rg --ignore-case --pretty --context 10 '$1' {}"
}
# Edit file with nvim or use fzf, through f(), to look for it, and then open it.
v() {
if [ -n "$1" ] && [ -f "$1" ]; then
# If the file exists, open it
$EDITOR "$1"
else
local pattern="$1"
# Expand tilde to $HOME if present
pattern="${pattern/#\~/$HOME}"
# Use fd if available for better performance
if command -v fd >/dev/null 2>&1; then
local file
# Use fd to find files matching the pattern, including hidden files
file=$(fd --hidden --no-ignore --type f --glob "$(basename "$pattern")*" "$(dirname "$pattern")" 2>/dev/null | \
fzf --query="$pattern" \
--preview "highlight -O ansi -l {} 2> /dev/null || cat {}")
else
# Fallback to find if fd is not available
local file
file=$(find "$(dirname "$pattern")" -type f -iname "$(basename "$pattern")*" 2>/dev/null | \
fzf --query="$pattern" \
--preview "highlight -O ansi -l {} 2> /dev/null || cat {}")
fi
if [[ -n $file ]]; then
$EDITOR "$file"
else
# If no file is selected, open a new file with the given name
$EDITOR "$1"
fi
fi
}