-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_functions
144 lines (123 loc) · 3.61 KB
/
.bash_functions
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
#!/usr/bin/env bash
function mkdir_cd() {
### Create a dir (including parents) and then cd to it.
mkdir -p "$1" && cd "$1"
}
function mkdir_touch() {
### Touch AND make the dir recursively
local file_path="$1"
local dir_name=$(dirname "$file_path")
mkdir -p "$dir_name" && touch "$file_path"
}
function goat() {
local goat
goat=$(
command cat <<-EOF
(_(
/_/'_____/)
" | |
|""""""|
EOF
)
echo "$goat"
}
# rename terminal window title
function set-title() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*\a\]"
PS1=${ORIG}${TITLE}
}
function readable_path() {
tr ':' '\n' <<<"$PATH"
}
function every_binary() {
if [[ -x $(which exa) ]]; then
readable_path | xargs exa -alF --icons --header --extended
else
readable_path | xargs ls -la
fi
}
# Appropriate for lxde or such other tools
# Uses X11 so should work on any non-wailand thing
function set-title-x() {
local window_to_rename
window_to_rename="$(wmctrl -l -p \
| awk '{s = ""; for (i=5; i<= NF; i++) s= s " " $i ; print $3 "\t" s}' \
| fzf --header "[PID] [NAME] Select Window To Rename!" --header-lines=0)"
window_to_rename="$(echo "$window_to_rename" | awk '{ print $1 }')"
if [ -n "$1" ]; then
new_name="$1"
else
echo -n 'Input new name: '
read -r new_name
fi
xdotool search --onlyvisible --pid "$window_to_rename" --name "\a\b\c" set_window --name "$new_name"
}
# Call `unicode` with the given arguments, and then pipe to awk, printing the first field.
# Throws error if no args passed
function unicode-fzf() {
if [ -z "$1" ]; then
echo "No arguments passed"
return 1
fi
unicode --brief --max 0 "$@" | fzf | awk '{ print $1 }'
}
# These should really be their own git-something.sh functions
# But I'm lazy atm
is_in_git_repo() {
git rev-parse HEAD >/dev/null 2>&1
}
fzf_with_controls() {
fzf --ansi --no-sort --reverse --multi --bind 'alt-j:preview-down,alt-k:preview-up' \
--header 'Ctrl-j: Down, Ctrl-k: Up Alt-j:preview-down,Alt-k:preview-up' --preview "$@"
}
_gf() {
is_in_git_repo || return
git -c color.status=always status --short \
| fzf_with_controls 'batdiff --color {-1} | head -500' \
| cut -c4- | sed 's/.* -> //'
}
_gt() {
is_in_git_repo || return
git tag --sort -version:refname \
| fzf_with_controls 'git show --color=always {}'
}
_gh() {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always "$@" \
| fzf_with_controls 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -500' \
| command grep -o "[a-f0-9]\{7,\}"
}
fkill() {
local pid
if [ "$UID" != "0" ]; then
pid=$(command ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
else
pid=$(command ps -ef | sed 1d | fzf -m | awk '{print $2}')
fi
if [ "x$pid" != "x" ]; then
echo $pid | xargs kill -${1:-9}
fi
}
killport() {
# source https://news.ycombinator.com/item?id=35698782
lsof -ti :"$1" | xargs kill -9
}
########################################
# #
# FUZZY SEARCH FUN #
# #
########################################
# Use fd (https://github.com/sharkdp/fd) instead of the default find
# command for listing path candidates.
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
_fzf_compgen_path() {
fd --hidden --follow --exclude ".git" . "$1"
}
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git" . "$1"
}