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:
-
Make sure you are in your home directory: cd ~
-
Check all the files in your home directory: ls -a
-
Look for the following file: .bash_aliases
-
If .bash_aliases doesn't exist, then make it: touch .bash_aliases
-
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):
-
-
Edit the file by pressing your [i] button. This will put you in '-- INSERT --' mode.
-
Add your aliases.
-
To exit use the keyboard shortcut: [Ctrl] + [c]
-
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!
-
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