-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_shell_functions
99 lines (86 loc) · 3.06 KB
/
dot_shell_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
# Always list directory contents upon 'cd'
cd() { builtin cd "$@"; ll; }
# kill process by specifying the port e.g. "killport 3000"
killport() {
port=$(lsof -n -i4TCP:$1 | grep LISTEN | awk '{ print $2 }') kill -9 $port
}
# Extract most know archives with one command
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Compare original and gzipped file size
gz() {
local origsize
origsize=$(wc -c < "$1")
local gzipsize
gzipsize=$(gzip -c "$1" | wc -c)
local ratio
ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l)
printf "orig: %d bytes\\n" "$origsize"
printf "gzip: %d bytes (%2.2f%%)\\n" "$gzipsize" "$ratio"
}
# Get colors in manual pages
man() {
env \
LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
LESS_TERMCAP_md="$(printf '\e[1;31m')" \
LESS_TERMCAP_me="$(printf '\e[0m')" \
LESS_TERMCAP_se="$(printf '\e[0m')" \
LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
LESS_TERMCAP_ue="$(printf '\e[0m')" \
LESS_TERMCAP_us="$(printf '\e[1;32m')" \
man "$@"
}
squash() {
if [ $(git rev-parse --abbrev-ref HEAD) = "master" ]; then
echo "you're on master, definitely don't want to run this."
return
fi
git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD))
git add -A
git commit -m $1
}
trash () { command mv "$@" ~/.Trash ; } # trash: Moves a file to the MacOS trash
# tmux, be there and be named well
function tms {
local name=$(basename $PWD | sed -e s/\[^a-zA-Z0-9\\\//\$]/-/g -e s/--*/-/g)
tmux new -s $name || tmux attach-session -t $name
}
port() { lsof -iTCP:$1 -sTCP:LISTEN; }
port2() { nc -zv localhost $1; }
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
open "http://localhost:${port}/"
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# Docker: bash into an existing container
dkbash() { docker exec -it $1 /bin/bash; }
# Docker: logs of an existing container
dklog() { docker logs -f $1; }
# Docker: list the names of existing containers
dknames() { docker ps --format '{{.Names}}' }
# Test shell load times
shelltime() {
shell=${1-$SHELL}
for i in $(seq 1 10); do /usr/bin/time $shell -i -c exit; done
}