Some Stuff

because Engineering is not just a profession, it's a life philosophy

More Useful .bashrc Tips

More useful snippets for your ~/.bashrc file to increase the efficiency of your everyday terminal tasks.

Tree grep

This will grep every file found recursively starting from the current directory. Will accept multiple search expressions and will print the results for each search term separately with nice colors. Very useful for searching through large collection of code or other text, especially when searching for multiple things.

1
2
3
4
5
6
7
8
9
10
11
tgrep() {
  if [[ $# < 1 ]] ; then
    echo "No pattern provided"
    return 1
  fi
  while [[ $# > 0 ]] ; do
    echo -e "\e[105m == ${1} == \e[49m"
    find . -type f -print0 | xargs -0 -IPATH grep --color=auto -Hn -e ${1} "PATH"
    shift
  done
}

Update: A better option is to use ag (the silver searcher).

Print exit code of the last command

If the last command exited with a non zero return code then the return code and, when applicable, the accompanying signal is printed to the terminal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# error code printing on command exit
function prompt-show-status ()
{
  # Don't use `status'.  It's read-only in zsh.
  local stat="$?"

  if test $stat -ne 0 -a $stat != 128; then
    # If process exited by a signal, determine name of signal.
    if test $stat -gt 128; then
      local signal="$(builtin kill -l $[$stat - 128] 2>/dev/null)"
      test "$signal" && signal=" ($signal)"
    fi
    echo -e "\033[01;31m[Exit $stat$signal]\033[00m" 1>&2
  fi
  test "$BASH" && history -a
  return 0
}

# Show status of the last executed command
PROMPT_COMMAND=prompt-show-status

Strip colors from output

Pipe the results through this command to strip colors from the output of other commands.

1
alias stripcolors='sed "s/\x1B\[\([0-9]\{1,2\}\(;[0-9]\{1,2\}\)\?\)\?[mGK]//g"'