forked from mitchallain/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_aliases.sh
389 lines (332 loc) · 15.4 KB
/
bash_aliases.sh
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/bash
# ---------------------------------------------------------------------------
#
# Description: This file holds default arguments, aliases, and custom functions
#
# Sections:
# 1. Environment Configuration
# 2. Default Options and Useful Aliases
# 3. ROS and Robotics Development
# 4. Specialized Tools
# 4. Searching
# 5. Process Management
# 6. Networking
# 7. System Operations & Information
# 9. Reminders and Notes
#
# ---------------------------------------------------------------------------
# ----------------------------------------
# 2. DEFAULT OPTIONS AND USEFUL ALIASES
# ----------------------------------------
alias cp='cp -iv' # prompt before overwrite (-i) and verbose (-v)
alias mv='mv -iv' # prompt before overwrite (-i) and verbose (-v)
alias nt='nautilus' # nautilus file browser shortcut
alias mkdir='mkdir -pv' # make parents (-p) and verbose (-v)
# show dotfiles (-A), classify (-F), no-group (-G), human readable sizes (-h)
# long listing (-l), append / to directories (-p), sort by mtime newest first (-t)
alias ll='ls -AFGhlp'
alias llt='ls -AFGhlpt'
# clear screen (-c), exit if only one screen (-F), raw control chars (-R)
# truncate long lines (-S), no termcap init (-X)
alias less='less -cFRSX'
alias cd..='cd ../' # Go back 1 directory level (for fast typers)
alias ..='cd ../' # Go back 1 directory level
alias ...='cd ../../' # Go back 2 directory levels
alias .3='cd ../../../' # Go back 3 directory levels
alias .4='cd ../../../../' # Go back 4 directory levels
alias .5='cd ../../../../../' # Go back 5 directory levels
alias .6='cd ../../../../../../' # Go back 6 directory levels
alias ~="cd ~" # ~: Go Home
alias c='clear' # c: Clear terminal display
alias which='type -a' # which: Find executables
alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths
alias fix_stty='stty sane' # fix_stty: Restore terminal settings when screwed up
mcd () { mkdir -p "$1" && cd "$1"; } # mcd: Makes new Dir and jumps inside
alias DT='tee /tmp/termout.txt' # DT: Pipe content to temporary file
alias xclipc="xclip -selection c"
alias xclipcr="tr -d '\n' | xclip -selection c"
alias xclipv="xclip -selection clipboard -o"
## Git aliases
# note git <alias> type commands can be defined in .gitconfig
alias gitl="git log"
alias gits="git status"
# bd - back to parent directory
# https://github.com/vigneshwaranr/bd
alias bd=". bd -si"
## COMMAND ALTERNATIVES
## Replace some common commands with fancier alternatives from this century
# bat - better cat
if [ -x "$(command -v bat)" ]; then
alias cat="bat"
fi
# exa - better ls
if [ -x "$(command -v exa)" ]; then
alias lx="exa -lga --icons -s name"
fi
## BASH FUNCTIONS
## --------------
# trash : alternative to rm which moves all arguments to the Ubuntu trash
# : TODO: platform-agnostic
# ------------:-----------------------------------------------------------------
trash () { command mv "$@" ~/.local/share/Trash ; }
# xopen : xdg-open wrapper which supports multiple files/urls and runs in
# : bg by default
# ------------:-----------------------------------------------------------------
xopen() {
for var in "$@"; do
(xdg-open "$var" > /dev/null 2>&1 &)
done
}
# savetmuxbuffer : captures the last 10000 lines from tmux and saves to a file
# : requires three args: session, window, and pane
# ----------------:-------------------------------------------------------------
# Example : 'savetmuxbuffer ROS bringup 0'
savetmuxbuffer () {
/usr/bin/tmux send-keys -t $1:$2.$3 C-b ":capture-pane -e -S 10000" C-m
/usr/bin/tmux send-keys -t $1:$2.$3 C-b ":save-buffer ~/buffer.txt" C-m
}
# lr : List n most recently modified files in directory
# ------------:-----------------------------------------------------------------
# Example : 'lr 5'
lr () {
ll -t | head -n $1
}
# mans : Search manpage given in arg 1 for term in arg 2 (case insensitive)
# : Displays paginated & colored result with two lines per hit
# ------------:-----------------------------------------------------------------
# Example : 'mans mplayer codec'
mans () {
man $1 | grep -iC2 --color=always $2 | less
}
# colview : view argument as formatted csv file in the console
# ------------:-----------------------------------------------------------------
# Example : 'colview test.csv'
colview () {
column -n -s, -tn < $1 | less -#2 -N -S
}
# unmvgitmv : move a file at arg 1 back to path arg 2, then git mv from 2 to 1
# : for when you forget to git mv
# ------------:-----------------------------------------------------------------
unmvgitmv () {
mv $1 $2
git mv $2 $1
}
# ----------------------------------------
# 3. ROS AND ROBOTICS DEVELOPMENT
# ----------------------------------------
# Preferred pylint config
alias pylint="pylint --rcfile=~/.pylintrc"
# # ROS aliases
alias rosenv='printenv | grep ROS' # rosenv: print the ROS path variables
alias rosgdb="rosrun --prefix 'gdb -ex run --args' "
alias rosprofile="rosrun --prefix 'valgrind --tool=callgrind' "
alias rosmemcheck="rosrun --prefix 'valgrind --tool=memcheck' "
# rosloglvl : set logger in arg 1 at node in arg 2 to level in arg 3
# ------------:-----------------------------------------------------------------
# Example : 'rosloglvl ultra_workspace_manager rospy debug' (confirm?)
rosloglvl () {
rosservice call $2/set_logger_level "logger='ros.$1'"$'\r'"level='$3'"
}
# sws : reset ROS environment, find containing workspace and source it
# ------------:-----------------------------------------------------------------
sws () {
source /opt/ros/$ROS_DISTRO/setup.bash;
local wspath=`catkin locate`;
source $wspath/devel/setup.bash;
}
# rostopicw : update rostopic list every 1 second, or n seconds where n is arg 1
# ------------:-----------------------------------------------------------------
rostopicw () {
local period=${1:-1}
watch -n $period "rostopic list"
}
# rostopicw : update rosnode list every 1 second, or n seconds where n is arg 1
# ------------:-----------------------------------------------------------------
rosnodew () {
local period=${1:-"1"}
watch -n $period "rosnode list"
}
# rosmaster : replace rosmaster hostname with arg 1, assume default port
# ------------:-----------------------------------------------------------------
rosmaster () {
export ROS_MASTER_URI=http://$1:11311
}
# rosgrep : find package
# : todo replace with fuzzy package finder
# ------------:-----------------------------------------------------------------
rosgrep () {
rospack list | grep "$1";
}
# roseulerify : republish a transform as Vector3 with rpy components
# : TODO better way?
# ------------:-----------------------------------------------------------------
roseulerify () {
rosrun topic_tools transform $1 /eulerify$1 geometry_msgs/Vector3 'map(lambda x: x * 57.296, tf.transformations.euler_from_quaternion([m.x, m.y, m.z, m.w]))' --import tf
}
# roswiki : parse ros wiki url from package.xml and open in chrome
# ------------:-----------------------------------------------------------------
roswiki () {
roscd $1; URL=$(cat package.xml | grep http | cut -d ">" -f 2 | cut -d "<" -f 1); /opt/google/chrome/chrome $URL
}
# catkin_test : build package name from first arg and execute tests, printing results
# : results are printed ...
# : TODO is this redundant with 'catkin test --this'?
# ------------:-----------------------------------------------------------------
catkin_test() {
local wspath=`catkin locate`
catkin build --no-deps --catkin-make-args run_tests -- $1 > /dev/null && catkin_test_results $wspath/build/$1
# catkin build --no-deps --catkin-make-args run_tests -- $1 && catkin_test_results $wspath/build/$1
}
# catkin_test_this : build current package and execute tests, printing results
# -----------------:-----------------------------------------------------------------
catkin_test_this() {
local pkgname=`catkin list --this -u`
catkin_test $pkgname
}
# catkin_source : source containing ws *without* clearing ros env
# : i.e., this will overlay the current workspace
# -----------------:-----------------------------------------------------------------
catkin_source() {
source `catkin locate`/devel/setup.bash
}
# catkin_tidy : uses compdb tool to collect all built packages' translation dbs
# : and links to src directory for editor to run clang-tidy against
# : TODO check compdb installation first
# -----------------:-----------------------------------------------------------------
catkin_tidy() {
catkin build $1 --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
local wspath=`catkin locate`
# local translation_db=$wspath/build/$1/compile_commands.json
local packages=`catkin list -u`
local dbs=""
for pack in $packages
do
if [ -f $wspath/build/$pack/compile_commands.json ]; then
local db="-p $wspath/build/$pack "
dbs="$dbs$db"
fi
done
compdb $dbs list > $wspath/src/compile_commands.json
# old, todo remove, linking individual packages, before I found compdb
#if [ -f $translation_db ]; then
# ln -fs $wspath/build/$1/compile_commands.json $wspath/src/compile_commands.json
#else
# echo "$translation_db does not exist"
#fi
}
rosbag_simtime() {
rosparam set /use_sim_time true
rosbag play $1 -l --clock
}
# Private alias definitions for work - stored separately
if [ -f ~/.aliases/private_aliases.sh ]; then
. ~/.aliases/private_aliases.sh
fi
# fzf dependent functions and aliases
if [ -x "$(command -v fzf)" ] && [ -f ~/.aliases/fzf_functions.sh ]; then
. ~/.aliases/fzf_functions.sh
fi
# MAC OS X Aliases - TO DO
# ------------------------
# ql () { qlmanage -p "$*" >& /dev/null; } # ql: Opens any file in MacOS Quicklook Preview
# -------------------------------
# 4. SPECIALIZED TOOLS
# -------------------------------
# DOCKER ALIASES
alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)'
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'
# MERMAID DIAGRAM GENERATION
alias mmdc="/home/mallain/dev/node_modules/.bin/mmdc"
# mmdclip : use text in clipboard to generate mermaid diagrams
# : need to provide at least output path (-o PATH)
# : can provide any other args (see mmdc -h)
# -----------------:-----------------------------------------------------------------
# Example : 'mmdclip -w 1000 -o test.svg'
mmdclip() {
xclip -o | mmdc -i /dev/stdin -c ~/.mermaid.json $@
}
# extract: 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 ;;
*.tar.xz) 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
}
# ---------------------------
# 4. SEARCHING
# ---------------------------
alias qfind="find . -name " # qfind: Quickly search for file
ff () { /usr/bin/find . -name "$@" ; } # ff: Find file under the current directory
ffs () { /usr/bin/find . -name "$@"'*' ; } # ffs: Find file whose name starts with a given string
ffe () { /usr/bin/find . -name '*'"$@" ; } # ffe: Find file whose name ends with a given string
# ---------------------------
# 5. PROCESS MANAGEMENT
# ---------------------------
# findPid: find out the pid of a specified process
# -----------------------------------------------------
# Note that the command name can be specified via a regex
# E.g. findPid '/d$/' finds pids of all processes with names ending in 'd'
# Without the 'sudo' it will only find processes of the current user
# -----------------------------------------------------
find_pid () { lsof -t -c "$@" ; }
# memHogsTop, memHogsPs: Find memory hogs
# -----------------------------------------------------
alias mem_hogs_top='top -l 1 -o rsize | head -20'
alias mem_hogs_ps='ps wwaxm -o pid,stat,vsize,rss,time,command | head -10'
# cpuHogs: Find CPU hogs
# -----------------------------------------------------
alias cpu_hogs='ps wwaxr -o pid,stat,%cpu,time,command | head -10'
# # my_ps: List processes owned by my user:
# ------------------------------------------------------------
my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,command ; }
# ---------------------------
# 6. NETWORKING
# ---------------------------
# alias myip='curl ip.appspot.com' # myip: Public facing IP Address
# alias netCons='lsof -i' # netCons: Show all open TCP/IP sockets
# alias flushDNS='dscacheutil -flushcache' # flushDNS: Flush out the DNS Cache
# alias lsock='sudo /usr/sbin/lsof -i -P' # lsock: Display open sockets
# alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets
# alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets
# alias ipInfo0='ipconfig getpacket en0' # ipInfo0: Get info on connections for en0
# alias ipInfo1='ipconfig getpacket en1' # ipInfo1: Get info on connections for en1
# alias openPorts='sudo lsof -i | grep LISTEN' # openPorts: All listening connections
# alias showBlocked='sudo ipfw list' # showBlocked: All ipfw rules inc/ blocked IPs
# # ii: display useful host related informaton
# # -------------------------------------------------------------------
# ii() {
# echo -e "\nYou are logged on ${RED}$HOST"
# echo -e "\nAdditionnal information:$NC " ; uname -a
# echo -e "\n${RED}Users logged on:$NC " ; w -h
# echo -e "\n${RED}Current date :$NC " ; date
# echo -e "\n${RED}Machine stats :$NC " ; uptime
# echo -e "\n${RED}Current network location :$NC " ; scselect
# echo -e "\n${RED}Public facing IP Address :$NC " ;myip
# #echo -e "\n${RED}DNS Configuration:$NC " ; scutil --dns
# echo
# }
# prints an excerpt from the art of the command line
function taocl() {
curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md |
sed '/cowsay[.]png/d' |
pandoc -f markdown -t html |
xmlstarlet fo --html --dropdtd |
xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
xmlstarlet unesc | fmt -80 | iconv -t US
}