# Persisting Aliases in Command Lines Using shell aliases in the command line can help streamline tasks repeated on a day-to-day basis. An alias replaces a command with an alternative, usually shorter, command. A catch is that aliases last until the current shell session ends. This vignette reminds me of how I can create and *persist* aliases for command-line interpreters---like the Git Bash terminal. ## Statement of Need Using aliases in the command line can save time by using custom shorthands. For example, instead of typing out `ls -al` every time to view the contents of a directory in a long format, I can create an alias called `ll` that performs the same function. This can be especially useful for long or complex commands that are difficult to remember. ## Example of Git Aliases An example of an alias is in the Git version control workflow. A common alias in Git is to add a `last` command which is the short form of `git log -1 HEAD` [like this](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases): ```bash $ git config --global alias.last 'log -1 HEAD' $ git last ``` ```text commit 66938dae3329c7aebe598c2246a8e6af90d04646 Author: Josh Goebel Date: Tue Aug 26 19:48:51 2008 +0800 Test for current head Signed-off-by: Scott Chacon ``` instead of doing ```bash git log -1 HEAD ``` to get the same information. Aliases potentially save a lot of time for tasks that are done frequently. Another example (outside of Git) is if I'm in a directory with a text file, like a `setup.py`, and I want to edit it. I can use the mouse to navigate that same path, double-click, and open the text file to edit it. **Aggravation #1**: This can sometimes be one too many keystrokes and mouse clicks. **Solution**: I can just do ```bash $ notepad setup.py ``` (or whatever is the notepad-equivalent text editor shipped with the OS) right there in the directory from the command line. But for another text editor like the Sublime Text editor, I need to define an alias for it to open `setup.py`. ## Create an Alias I can create an alias for Sublime Text by doing ```bash $ alias subl="/d/Program\ Files/Sublime\ Text\ 3/sublime_text.exe" ``` where `subl` is the new alias I (think I) want for the Sublime Text editor. `"/d/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"` is the path of the Sublime Text executable file. To confirm that the alias is registered, do a ```bash $ alias alias ll='ls -l' alias node='winpty node.exe' ... alias subl='/c/Program\ Files/Sublime\ Text/sublime_text.exe' ``` I can now edit `setup.py` using Sublime Text by doing ```bash $ subl setup.py ``` **Aggravation #2**: Unfortunately, an alias lasts only the life of the shell session. Once I exit Git Bash, the alias is gone. The next time I open the command line again and do a `subl` I get the rude that message that: ```bash $ subl setup.py bash: subl: command not found ``` ## Persist the Alias (Pt. 1) **Solution**: Persist the new alias by adding it to the `bashrc` file. Open the `bashrc` file ```bash $ notepad ~/.bashrc ``` and append `alias subl="/d/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"` to the end of the file. Save and close. So the next time I open Git Bash my alias still works. **Aggravation #3**: I later discovered that the above solution did not work on another machine that also uses Windows OS + Git Bash. So defining new aliases in `bashrc` did not work on my other machine. An alternative is to do a `source ~/.bashrc` every time I start a Git Bash session so that the aliases are sourced from the `bashrc` file. But that is again too many keystrokes and an extra command to remember. On further investigation, it turns out that Git Bash does not start the shell using the `~/.bashrc` file. It uses the `/etc/profile` file as well as *one* of the three following files (if they exist) in order of precedence: * `~/.bash_profile` * `~/.bash_login` * `~/.profile` The `~/.bashrc` file, and the aliases definitions within are thus ignored. ## Persist the Alias (Pt. 2) **Solution**: The fix that finally ended up working for me is to open the `~/.bash_profile` file using ```bash $ notepad ~/.bash_profile ``` and appending `source ~/.bashrc` to the end of the file. Save and close. The aliases will now work every time I start a new terminal session by sourcing the aliases from ~/.bashrc on start-up. ## Multiple Aliases for the Same Command **Aggravation #4**: Apparently, I forgot what alias I defined for Sublime Text more than once and ended up defining multiple aliases for the same command. Multiple aliases for the same command is ok. **Solution**: For example, I can define multiple aliases for the same Sublime Text editor by adding ```bash alias sblt="C:/Program\ Files/Sublime\ Text/sublime_text.exe" alias subl="C:/Program\ Files/Sublime\ Text/sublime_text.exe" alias sublime="C:/Program\ Files/Sublime\ Text/sublime_text.exe" ``` to the `~/.bashrc` file. ## Aliases of Aliases It's also possible to define multiple aliases as ```bash alias sblt="C:/Program\ Files/Sublime\ Text/sublime_text.exe" alias subl=sblt alias sublime=sblt ``` (It's [turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_down#/media/File:River_terrapin.jpg).) ## Resources * [Git Basics - Git Aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases) * [Why doesn't .bashrc run automatically?](https://apple.stackexchange.com/questions/12993/why-doesnt-bashrc-run-automatically) * [Why must I source .bashrc every time I open terminal for aliases to work?](https://stackoverflow.com/questions/51876792/why-must-i-source-bashrc-every-time-i-open-terminal-for-aliases-to-work)