My .bash_aliases



Hey, fellow developers!

So this post is more for IT people, but for other I will explain what aliases are. Bash is a terminal shell - basically a framework which can interpret your commands, execute them and give the output. Bash is only one of shells like sh, zsh, fish, kzsh, tcsh, etc.

Being a developer involves quite a lot of work with a terminal, thus typing many commands a day. Typing a command takes time. Moreover, when you have to type it many times in a row or in a short period of time, it might get really annoying (at least for me 🙂). However, there is a way to speed it up and ease the typing - aliases. Aliases allow you to name a certain longer command and run it next time just by typing the name.

So without further due, here is my list current aliases:

Docker

  • alias dr="docker-compose run"
  • alias de="docker-compose exec"
  • alias db="docker-compose build"

Composer

  • alias c='composer'
  • alias t='c test' - I like to put a command of running tests a into a composer.json as a script so everybody would be running them the same way. Usually it is already prepared command for running tests in a parallel thus increasing the speed.
  • alias cr='c require'
  • alias crd='cr --dev'
  • alias cu='c update'
  • alias cud='cu --dry-run'
  • alias ci='c install'

Git

  • alias g='git'
  • alias gs='g status' - a quick way to check if the branch is clean, or what was changed
  • alias gc='g commit -m' - for those who like git in terminal,  this is a tiny helper for commiting. Just type gc and commit message.
  • alias gpl='g pull'
  • alias gps='g push'
  • alias gpd='gps --delete' - quickly delete a remote branch
  • alias gf='g fetch'
  • alias gt='g tag'
  • alias gtd='gt -d'
  • alias dracarys='g reset --hard && g clean -df' - this one is funny 😀. A reference to Game of Thrones series. Will destroy all unsaved changes. Found it somewhere on the Internet.
  • alias gl="g log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" - as I know this one is used by Laravel creator Taylor Otwell. A neat way to view git commit history in a terminal with better colors and formatting.

Etc.

  • alias source-zshrc='source ~/.zshrc' - reload terminal shell (zsh) configuration
  • alias aliases='subl ~/.bash_aliases' - edit .bash_aliases file in Sublime
  • alias zshrc='subl ~/.zshrc' - edit terminal shell (zsh) configuration
  • alias mt4='nohup bash -c "wine \"~/.wine/drive_c/Program Files/MetaTrader 4/terminal.exe"\" > /dev/null 2>&1 &' - this one allows me to quickly run a certain Windows application and allowing me to use the same terminal session further.

Now sometimes, having just an alias is not enough. Sometimes, you need an alias with varying parts in the middle. To achieve that, we can use functions. Here are those I have:

function dr-php() {
    container="$1";
    shift;
    dr "$container" php "$@";
}

function dr-artisan() {
    container="$1";
    shift;
    dr-php "$container" artisan "$@";
}

function dr-composer() {
    container="$1";
    shift;
    dr "$container" composer "$@";
}

function dr-npm() {
    container="$1";
    shift;
    dr "$container" npm "$@";
}

function dr-bower() {
    container="$1";
    shift;
    dr "$container" bower "$@";
}

function dr-bash() {
    dr "$1" bash;
}

function de-php() {
    container="$1";
    shift;
    de "$container" php "$@";
}

function de-artisan() {
    container="$1";
    shift;
    de-php "$container" artisan "$@";
}

function de-composer() {
    container="$1";
    shift;
    de "$container" composer "$@";
}

function de-npm() {
    container="$1";
    shift;
    de "$container" npm "$@";
}

function de-bower() {
    container="$1";
    shift;
    de "$container" bower "$@";
}

function de-bash() {
    de "$1" bash;
}

function file-watcher() {
    if [ $# -ne 2 ]; then
        echo "Usage: $0 CODE-DIRECTORY COMMAND"
        exit 1
    fi

    code_directory=$1

    if [ ! -d $code_directory ]; then
        echo "Error: invalid dir $code_directory"
        exit 1
    fi

    while inotifywait -qq -e close_write -r $code_directory --exclude=".*sw[px]"; do
        clear && $2
    done
}

That's it for now 🙂. Do you have some useful aliases you use? Please share them in the comments. They will be appreciated!


Comments

Popular posts from this blog

Surround yourself with smarter than you

Redis smooth throttling

Fast PHPUnit tests