{ ^_^ } sinustrom Solving life, one problem at a time!

More useful .bashrc tips

Author: Zoltan Puskas
Categories: linux

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.

tgrep() {
  if [[ $# < 1 ]] ; then
    echo "No pattern provided"
    return 1
  while [[ $# > 0 ]] ; do
    echo -e "\e[105m == ${1} == \e[49m"
    find . -type f -print0 | xargs -0 -IPATH grep --color=auto -Hn -e ${1} "PATH"

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

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.

# 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)"
    echo -e "\033[01;31m[Exit $stat$signal]\033[00m" 1>&2
  test "$BASH" && history -a
  return 0

# Show status of the last executed command