-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_fzf
120 lines (97 loc) · 2.46 KB
/
.bash_fzf
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
# fkill() - kill a process from a ps tree
fkill() {
local plist
if [[ "$UID" != "0" ]] ; then
plist=$(ps -f -u $UID)
else
plist=$(ps -ef)
fi
local pid
pid=$(echo "$plist" | sed 1d | fzf --prompt="Kill process(es) > " --tac -m | awk '{ print $2}')
if [[ "x$pid" != "x" ]] ; then
history -s kill $pid
kill $pid
fi
}
# fd() - cd to selected directory
fd() {
local query
if [[ ! -z "$@" ]] ; then
query="--query=$@"
fi
local dir
dir=$(find . -type d -print 2> /dev/null | fzf $query --prompt="Change into directory > ")
if [[ "x$dir" == "x" || "$dir" == "." ]] ; then
echo "No directory below current"
return
fi
history -s cd $dir
cd $dir
}
# ssh to an IP selected from aws-list-ec-instances --region=us-west-2 --table
fssh() {
local instline
instline=$(aws-list-ec2-instances --region=us-west-2 --table|fzf --header-lines=2 --prompt="SSH into AWS EC2 instance > ")
if [[ "x$instline" != "x" ]] ; then
local ip
ip=$(echo $instline | tr -d ' ' | awk -F '|' '{ print $4 }')
if [[ "x$ip" != "x" ]] ; then
echo "Ssh'ing into instance below"
echo
echo "$instline"
echo
history -s "ssh $ip"
ssh $ip
else
echo "Can't ssh into instance below because it has no external IP address"
echo
echo "$instline"
fi
fi
}
# fe - find file and run an editor
fe() {
local file
IFS=$'\n' file=($(fzf --prompt="Edit file > " --query="$1" --select-1 --exit-0))
if [[ -n $file ]] ; then
local cmd
history -s ${EDITOR} $file
${EDITOR} $file
fi
}
# run ack-grep and trigger editing a selected file
vg() {
if [[ -z "$@" ]] ; then
echo "vg: need a string to look for"
return
fi
local file
local line
local txt=$(ack --nobreak --noheading $@ | fzf --prompt="Edit file > " -0 -1 | awk -F':' '{ print $1":"$2}')
local file=$(echo "$txt"| awk -F':' '{ print $1 }')
local line=$(echo "$txt"| awk -F':' '{ print $2 }')
if [[ -n $file ]] ; then
history -s ${EDITOR} +${line} ${file}
${EDITOR} +${line} ${file}
fi
}
# cd into a directory of a file
cdf() {
local file
local dir
file=$(fzf --prompt="Change into directory of a file > " --query="$1")
if [[ "x$file" == "x" ]]; then
echo "No file selected"
return
fi
dir=$(dirname "$file")
if [[ "$dir" == "." ]] ; then
return
fi
if [[ "x$dir" == "x" ]] ; then
echo "Can't extract a directory from $file"
return
fi
history -s cd $dir
cd $dir
}