HomeProjectsCV
  
  

Using Git Bash Aliases to Automate Your Windows Development Environment

April 3, 2020

Like many other software developers, I work on the same web app every day. I have to open up my project directory in VS Code, run my server, fire up Google Chrome, and then launch an incognito window to ensure that I'm working with the purest possible version of my web app. Sure, running this process manually doesn't take that long, but each microtask is a context shift that can take your focus away from the actual work you're getting ready to do. In this tutorial, I'm going to help you save time and add more value to your projects by teaching you how to automate your setup process.

Note: This tutorial utilizes a Windows development environment and a Flask server, and assumes that you already have Git installed. You should be able to easily adapt it to your preferred environment since doing this in Windows just takes a few more steps than Mac/Linux users require.

Creating Git Bash Aliases

In order to automate our tasks, we are going to need to create some aliases. Bash aliases are shortcuts that allow you to perform a longer sequence of events with fewer keystrokes. You can name them whatever you want. With that said, you can apply what you learn here to as many projects and processes as you want!

To create our aliases, we're going to need to modify a file called .bash_profile (you can also use .bashrc, but that's a bit more complicated). You can find .bash_profile under your home folder/user profile, which should follow the pattern c/Users/<username>/.bash_profile.

So, fire open a terminal window and:

$ cd C:/Users/<your_user_dir>
$ ls -a

If you scroll up to the top, you will find your dotfiles and should see something similar to the following:

.
..
.bash_history
.bash_profile
.bashrc
.<some_other_files>

Congratulations! You've found your .bash_profile! Time to start hacking away at it.

Editing .bash_profile

Open up your .bash_profile in a simple text editor like so:

$ notepad .bash_profile

Note: .bash_profile is either blank or nonexistant by default, so if Notepad asks if you want to create it, go ahead and create it.

To get the feel for creating bash aliases, lets start with a classic "hello world" example:

# ~/.bash_profile
# comments go here
# ---------------------
# Command line aliases
# ---------------------
alias hello='echo hello world'

Now save your file and close Notepad. Open a new Git Bash terminal (your changes are not available unless you open a new window). You can now type hello into the console, and it will repeat 'hello world' right back at you:

$ hello
> hello world

Now that you understand the basics of creating aliases, lets get to the real fun!

Including VS Code in Your Git Bash Path

By default, VS Code adds its the code CLI to your path for Windows, but you have to do this process separately for Git Bash (hence our use of Notepad previously). code allows us to open up files and workspaces from the command line with commands like code -r <file_name> and code . --new-window.

To make code accessible from Git Bash, add the following to your .bash_profile:

alias code='cmd //C code $*'

Opening Your Workspace in VS Code

Add the following line to launch your project in a new VS Code workspace:

alias open-proj='cd /c/<path>/<to>/<your>/<project>/<project_directory> && code . --new-window'

Run open-proj in Git Bash and see your workspace launch in VS Code!

Running Your Server & Opening an Incognito Window

This part depends entirely on your application, so you'll have to tailor this one a bit more. The basic syntax is as follows:

alias serve='cd /c/<path>/<to>/<your>/<project>/<project_directory> && <command_to_run_server>'

A generic example might be:

alias serve='cd /c/<path>/<to>/<your>/<project>/<project_directory> && start chrome http://127.0.0.1:8080/ --incognito'

Tying it All Together

String what you've learned together as follows:

alias run='start chrome http://127.0.0.1:8080/ --incognito && open-proj && serve'

Conclusion

You now have everything you need to get your development environment started in just a few keystrokes!

The final contents of your file should look something like this, with your own path and server command:

# invoke vscode command `code`
# NOTE: This alias must precede any aliases
# that utilize it
alias code='cmd //C code $*'
# open the project directory in new vs code workspace
alias open-proj='cd /c/Users/Benjamin/Desktop/repos/my-proj && code . --new-window'
# run the project server -- a python app used as an example
alias serve='cd /c/Users/Benjamin/Desktop/repos/my-proj && python run.py'
# open my-proj in vs code, start the server,
# and open it in an incognito window
alias run='start chrome http://127.0.0.1:5000/ --incognito && open-proj && serve'
Benjamin Kleeman

Benjamin Kleeman

Software Developer & Musician

 

© 2020