DEV Community

Discussion on: What are your preferred bash aliases?

Collapse
 
eljayadobe profile image
Eljay-Adobe • Edited

I like to make my ~/.bash_profile able to be re-sourced multiple times. Because I'll change it, and then re-source it. But I don't want my PATH to become super long with duplicates. So I check PATH before appending or prepending a path.

[[ ":${PATH}:" == *":/usr/local/bin:"* ]] || PATH="/usr/local/bin:${PATH}"
Enter fullscreen mode Exit fullscreen mode

I use SCOWL to look up words frequently. So I like to be able to grep words no matter where I am.

function scowl {
  pushd /Users/eljay/bin/scowl-2018.04.16/final >/dev/null
  grep "$@" *
  popd >/dev/null
}
Enter fullscreen mode Exit fullscreen mode

I'm often on a Macintosh using Terminal.app, and I like to be able to clear the screen and clear the scrollback buffer in one command.

alias cls="clear; printf '\033[3J'"
Enter fullscreen mode Exit fullscreen mode

I'm always making little C++ toy programs to test out a thought. I like to compile in a specific way, so a handy alias to the rescue.

export CXXWARN='-Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -pedantic -fsanitize=undefined,null,address,bounds,bool,enum'
alias c17='/usr/bin/clang++ ${CXXWARN} -std=c++17'
Enter fullscreen mode Exit fullscreen mode

I often want to go up directory levels or to HOME quickly.

alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ~='cd ~'
Enter fullscreen mode Exit fullscreen mode

When I do mkdir I often follow it with cd to that directory. So I've combined the operations. (Sometime I tweak it to be lazy and cd into the directory if it already exists. Sometimes I don't like that behavior, and take it out.)

function mkcd {
  if [ -n "$1" -a ! -a "$1" ]
  then
    mkdir "$1"
    cd "$1"
  else
    echo NO
  fi
}
Enter fullscreen mode Exit fullscreen mode

Sometimes I'm editing an HTML file (and possibly associated CSS and JavaScript files), and I want Chrome to reload my local page for me when it changes. (I think I got fswatch through brew.)

function watch {
    local DIR=$PWD

    if [ -n "$1" ]
    then
        DIR="$1"
    fi

    fswatch -o "$DIR" | xargs -n1 -I {} osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'
}
Enter fullscreen mode Exit fullscreen mode