-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.zsh
223 lines (212 loc) · 6.85 KB
/
aliases.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
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
#!/usr/bin/env zsh
# Go to main dotfiles directory
alias dotfiles="cd $DOTFILES"
########## ALIASES #########
alias 'galias?=echo Search aliases using grep'
alias 'fa?=echo Search aliases by fuzzy search'
# Grep aliases
galias() {
params="$@"
alias | grep --colour=always "$params"
}
# Fuzzy search aliases
fa() {
dotfiles_export_aliases_prettyfied
alias=$(
echo $dotfiles_aliases_prettyfied |
fzf -m |
sed 's/\(.*\) \-\>.*/\1/')
if (type "$alias" > /dev/null); then
print -z $alias
fi;
}
alias vim=nvim
alias vi=nvim
########## HISTORY ##########
alias 'fh?=echo Search history by fuzzy search'
# fh - Search history
fh() {
print -z $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//')
}
########## SUDO ##########
# Sudo previous command
alias pls='sudo `fc -n -l -1`'
########## FILES ##########
alias 'fe?=echo Open with the default editor the file selected by fuzzy search'
# fe [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
fe() {
local files
IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
}
########## DIRECTORIES ##########
alias 'up?=echo cd .. n times'
alias 'fd?=echo cd to directory selected by fuzzy search'
alias 'fda?=echo cd to directory selected by fuzzy search including hidden ones'
alias 'cdf?=echo cd to parent directory of files selected by fuzzy search'
# Go UP n times: ex. 'up 2' means 'cd ../..'
up() {
if [[ "$#" < 1 ]]; then
cd ..
else
CDSTR=""
for i in {1..$1}; do
CDSTR="../$CDSTR"
done
cd $CDSTR
fi
}
# fd - cd to selected directory
fd() {
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) &&
cd "$dir"
}
# fda - including hidden directories
fda() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
}
# cdf - cd into the directory of the selected file
cdf() {
local file
local dir
file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
}
########## PROCESSES ##########
alias 'fkill?=echo Kill process selected by fuzzy search'
# fkill - kill process
fkill() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]; then
echo $pid | xargs kill -${1:-9}
fi
}
alias 'fpkill?=echo Kill process which is listening on port from Fuzzy search'
fpkill() {
pid=$(lsof -n -P -i | fzf -m | sed -E "s/^.*[[:space:]]+([0-9]+)[[:space:]]+.*/\1/")
if [ "x$pid" != "x" ]; then
echo $pid | xargs kill -${1:-9}
fi
}
########## GIT ##########
alias 'fbr?=echo Checkout git branch selected by fuzzy search'
alias 'fco?=echo Checkout git branch/tab selected by fuzzy search'
alias 'fcoc?=echo Checkout git commit selected by fuzzy search'
alias 'fshow?=echo Browse commits using fuzzy search'
alias 'fcs?=echo Get hash of commit selected by fuzzy search'
alias 'fstash?=echo Browse and diff stashes selected by fuzzy search'
alias 'current_branch?=echo Shows the name of the current branch'
alias 'current_repository?=echo Shows the name of the current repository'
alias 'work_in_progress?=echo Checks if the current branch is a WIP'
alias 'gfg?=echo List files in the index or the working tree matching given name'
alias ghsh="git log --pretty=format:'%h' -n 1"
alias gsst="git submodule status"
# fbr - checkout git branch (including remote branches)
fbr() {
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(
echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(
echo "$branch" |
sed "s/.* //" |
sed "s#remotes/[^/]*/##")
}
# fco - checkout git branch/tag
fco() {
local tags branches target
tags=$(
git tag |
awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all |
grep -v HEAD |
sed "s/.* //" |
sed "s#remotes/[^/]*/##" |
sort -u |
awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# fcoc - checkout git commit
fcoc() {
local commits commit
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fshow - git commit browser
fshow() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
# fcs - get git commit sha
# example usage: git rebase -i `fcs`
fcs() {
local commits commit
commits=$(git log --color=always --pretty=oneline --abbrev-commit --reverse) &&
commit=$(
echo "$commits" |
fzf --tac +s +m -e --ansi --reverse) &&
echo -n $(
echo "$commit" |
sed "s/ .*//")
}
# fstash - easier way to deal with stashes
# type fstash to get a list of your stashes
# enter shows you the contents of the stash
# ctrl-d shows a diff of the stash against your current HEAD
# ctrl-b checks the stash out as a branch, for easier merging
fstash() {
local out key sha
while out=$(
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf --ansi --no-sort --query="$q" --print-query \
--expect=ctrl-d,ctrl-b);
do
out=("${(f)${out}}")
key="${out[1]}"
sha="${out[-1]}"
sha="${sha%% *}"
[[ -z "$sha" ]] && continue
if [[ "$key" == 'ctrl-d' ]]; then
git diff $sha
elif [[ "$key" == 'ctrl-b' ]]; then
git stash branch "stash-$sha" $sha
break;
else
git stash show -p $sha
fi
done
}
########## VAGRANT ##########
alias 'vs?=echo List all vagrant boxes with statuses by fuzzy search and try to access the selected one via ssh'
vs() {
#List all vagrant boxes available in the system including its status, and try to access the selected one via ssh
cd $(
cat ~/.vagrant.d/data/machine-index/index |
jq '.machines[] | {name, vagrantfile_path, state}' |
jq '.name + "," + .state + "," + .vagrantfile_path'|
sed 's/^"\(.*\)"$/\1/' |
column -s, -t |
sort -rk 2 |
fzf |
awk '{print $3}');
vagrant ssh
}
########## HELPERS ##########
alias 'edit-command-in-editor?=echo "<CTRL>+x <CTRL>+e"'