Custom Command Line Functions

Improve your workflow by creating a functions file.


Creating a custom functions file where I can execute my own custom commands on the command line has been one of the biggest boosts to my productivity in the past year.

Thanks to Nirdosh Gautam whose Medium post How to create your own Custom Terminal Commands was a huge inspiration.

If you're a developer, then im my opinion this is a must. If you're an advanced mac/linux user, then this will probably also be a bit of fun for you.

So what am I on about?

In short this is a tutorial on how to start working on your projects by running a single command.
E.g. to start working on my blog, I open up terminal and write:

  
    work_blog
  

This opens up PHPStorm, my browser and uses yarn to start the project in dev mode.

Setup:

Before starting this, it's best to have VS Code installed
https://code.visualstudio.com/
This allows you to open files and projects using:

  
    code path/to/file
  

I would also recommend using oh-my-zsh for your terminal shell.
https://ohmyz.sh/

How To:

Create a hidden functions file in root:

  
    code ~/.my_functions.sh
  

create a function to edit your function file

  
    #!/bin/bash

    function my_functions() {
      code ~/.my_functions.sh
    }
  

save the file and make it executable

  
    chmod +x ~/.my_functions.sh
  

Add the source to your .zshrc file by:

Opening the file in VS Code:

  
    code ~/.zshrc
  

Then adding this:

  
    source ~/.my_functions.sh
  

To load the source file. In your terminal window execute:

  
    source ~/.zshrc
  

Now when you run the 'my_functions' command in your terminal, VS Code should start and you should be able to edit your commands.

Examples of functions that I use:

1. This function goes to my project in the terminal, opens up chrome on the dev server. Opens the project in PHPStorm and runs the dev server using 'Yarn Dev'

  
  function work_blog() {
    cd ~/home_projects/danownsthisspace
    open -a 'Google Chrome' http://localhost:3000
    pstorm .
    yarn dev
  }
  

I typically have a 'work_{project name}' function for all my projects. It allows me to not worry about remembering what is required to dev each project.

2. This simple function allows me to go to my previous git branch just by executing '--' in the terminal.

  
  function --() {
    git checkout -
  }
  

3. I also have these handy commands setup for my projects so that I quickly navigate to them or open them up in finder.

  
  function go_blog() {
    cd ~/home_projects/danownsthisspace
  }

  function open_blog() {
    open ~/home_projects/danownsthisspace
  }
  

4. For projects that require a few terminal windows I use a create_tab function to execute commands in multiple terminal windows

  
    function create_tab() {
      osascript &>/dev/null < "iTerm"
          activate
          tell current window to set tb to create tab with default profile
          tell current session of current window to write text "$1"
        end tell
    EOF
    }

    function work_clj_project() {
      go_clj_project
      create_tab 'lein repl' && create_tab 'lein figwheel' && create_tab 'lein auto sassc once'
    }
  

Repo:

I've created a starter file with some useful commands in it that you can pull to start your own my_functions.sh file

https://github.com/danownsthisspace/bash-functionss