-
Notifications
You must be signed in to change notification settings - Fork 1
/
general.bash
122 lines (94 loc) · 2.73 KB
/
general.bash
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
#### Private functions ####
_print_array()
{
local -n name=$1
printf "Valid options are"
for i in "${!name[@]}"
do
printf "\n- $i"
done
}
# $1 is the action to perform
# $2 is the associate array
# $3 is the key / selection
_execute()
{
local -n keys=$2
if [ $# -eq 3 ] && [ ${keys[$3]+exists} ]
then
"$1" "${keys[$3]}" && return 0
else
# TODO: only display keys prefixed with given string
_print_array keys && return 1
fi
}
_execute_notify()
{
START_TIME=$(date +"%r")
local -n keys=$2
if [ $# -eq 3 ] && [ ${keys[$3]+exists} ]
then
"$1" "${keys[$3]}"; echo "Sending Notification"; _send_notification "Execution complete" "${keys[$3]}\nTime: $START_TIME -$(date +"%r")" && return 0
else
# TODO: only display keys prefixed with given string
_print_array keys && return 1
fi
}
_execute_opt()
{
local -n keys=$2
if [ $# -eq 3 ] && [ ${keys[$3]+exists} ]
then
"$1" "${keys[$3]}"
fi
return 0
}
_execute_func()
{
local -n funcs=$1
if [ $# -eq 2 ] && [ ${funcs[$2]+exists} ]
then
"${funcs[$2]}"
fi
return 0
}
#### Utility ####
# Improved ls
alias ls="ls -FA"
# Different ls
alias ll="ls -lhFA"
# Improved grep
alias grep="grep --color "
# Search for a file with the name
alias fhere="find . -iname "
# Search for a file containing a string
alias shere="grep -rnw . -e "
# Display the file name containing the string (less detailed shere)
findh () { grep -Rl $1 .; }
# Replaces the contents and filename of str1 to str2
replace () { a=$1; b=$2; grep -Rl "$a" . | xargs sed -i -- s/$a/$b/g; }
# Create and enter directory
mkcd () { mkdir -p $1; cd $1; }
# Shortcut for up directory
alias ..="cd .."
# Opens the directory in Windows or Unix format. If no argument is given, open the current directory
exp () { if [ $# -eq 1 ]; then cygstart $1; else cygstart .; fi; }
# Display the space available on the HD
alias space="df -h"
# Improved rm for larger files TODO: clean up the need to create this enpty_dir/
alias rmsync="mkdir empty_dir; rsync -a --progress --delete empty_dir/ "
# Prints the contents of a function
display () { typeset -f "$1"; }
remove_spaces () { for f in *\ *; do mv "$f" "${f// /_}"; done; }
#### History ####
# Pressing space after !<command> or !! will show the command to be executed
bind Space:magic-space
# Increase HISTSIZE from 1000 to 10000
HISTSIZE=100000
HISTFILESIZE=110000
# Save timestamp in the history file
HISTTIMEFORMAT="%F %T "
# Don't store duplicates + ifnore commands starting with space
HISTCONTROL=ignoreboth
# Allow "sharing" of history between instances
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"