Creating aliases in Windows Subsystem for Linux

Purpose. The purpose of this document is to provide some basic information related to creating (permanent) aliases in Windows Subsystem for Linux (WSL). These concepts likely apply to straight Linux/Unix, but I haven't tested that out.

What is an alias? An alias is a kind of text-base short-cut when using terminal (Linux/Unix/Mac's command-line interface). For example, in terminal we could access a folder by a commands such as: cd /mnt/c/Blogging/blog (this folder is equivalent to C:\Blogging\blog on the Windows file system; cdchange directory). Using a graphical user interface we'd simply make a short-cut and place it somewhere convenient to avoid all the clicking or typing. But WSL is command-line only, so we are out of luck for that kind of solution.

I should also mention that aliasing is more powerful that just creating folder short-cuts. It's more like a (simple) function that can execute a number of command-line calls. Such calls might include logging onto Teton or some really verbose git command.

For the remainder of this document, I'm going to focus on two strategies for aliasing. The first focuses on an alias that you create for a single terminal session. The second focuses on a more permanent solution, such that an alias can be used over and over again.

The session-based solution to aliasing

The session-based solution is for something that is tedious, but only for a short-period of time. That is, when you close terminal, that session-based alias will no longer exist when you log back in.

The basic anatomy of creating an alias is: alias <your command>='<something you want to do>' (Note lack of spaces between the command and thing you want to do – spaces matter at the command-line.)

Example: alias project='cd /mnt/c/Projects/PaleoLakeElevations/scripts/r'. So, now whenever we type project into the command line we will be taken to the r folder. What this looks likes in terminal (I've omitted my user and computer names to protect my innocence):

The permanent solution to aliasing

Do not edit WSL files directly unless you really know what you are doing – especially not in Windows itself.

The more permanent solution to aliasing is editing a specific system file. This is a slightly more involved fix, but if you do the same command over and over again, it's pretty nice having a short-cut, of sorts.

The file we are going to focus on is .bash_aliases. Note that we could edit another file, .bashrc, to get the same outcome, but that file has some other important stuff in it that we don't want to screw up, so this is likely one of the safer routes to achieve our goal.

From terminal:

  1. Make sure you are in your home directory: cd ~
  2. Check all the files in your home directory: ls -a
  3. Look for the following file: .bash_aliases
  4. If .bash_aliases doesn't exist, then make it: touch .bash_aliases
  5. Once you've made or confirmed that .bash_aliases exists you want to open it up (I'm using the Vim text editor here): vi .bash_aliases
    • The file should look something like (but without the defined aliases):
  6. Edit the file by pressing your [i] button. This will put you in '-- INSERT --' mode.
  7. Add your aliases.
  8. To exit: [Ctrl] + [c] 
  9. Save your edits by typing: :wq! (This saves and closes the document, though this command convention is pretty foreign to me); To close without saving type: :qa!
  10. And just like that you are back at the terminal!

So if we run two of the commands from the above alias file we get the following:

The first command changes my directory to where I keep some files associated with a blog. The second command runs some Ruby-based Jekyll functions that allow me to preview the blog locally.

Other helpful commands

If you forget what your permanent aliases are, or what they do, below are a couple of commands that can help remind you.

Information for a single alias command.: type <name of alias> OR alias <name of alias>. Example: alias blog

Information for all aliases (both for what you have defined and what has been defined for you): alias