Syncing Aliases on Linux and Windows
I used to be somewhat hesitant to use aliases, because it got annoying when they weren't set on other servers. Still, I got slowly more and more annoyed with not using aliases, so I had to find a solution. This is what I came up with after a few tries.
My Requirements
I needed a simple solution that would work on all Linux servers, so it shouldn't require git or any specific programming language, which could disqualify some of our database servers. It should also work on my Windows PC's with Cmder, so it also works in my VSCode editor, because it's set up to Cmder as terminal.
- No requirements. Not even Git.
- One-line install / setup.
- Easy to edit aliases source.
- Always up-to-date (fast sync).
- Works for both Linux (Ubuntu) and Windows Cmder.
My Solution
I decided to use GitHub's Gist for my aliases, because it can render "raw" files, and it's simple, fast, has revisions and secure (2FA) - but you can use any online service you'd like. Pastebin and Gitlab Snippets also works.
The aliases are then updated on each SSH login from the remote source (Gist), and on Windows, your aliases will be updated when you start Cmder.
Notice
- The Gist has to be public, so the servers can fetch the content without authorization (it's just aliases, who cares).
- Gist caches files for a couple minutes, so if your aliases aren't showing, just try again in 2-3 minutes.
- On Linux, it overwrites your
~/.bashrc
file, if it exists. -
macOS: This has not been tested on macOS, but it seems
wget
is not default installed. Instead, you can usecurl -O
(or install wget) - just know some Linux servers may not havecurl
installed. I choosewget
because all Ubuntu servers has this by default, and in my testingwget
was a slightly faster thancurl
.
Step 1 - Creating Your Aliases
Create a new Gist and name the filename .bash_aliases
Then, add in all your aliases in Linux style, e.g.
alias ll='ls -alh --show-control-chars -F --color'
alias gl='git log --oneline --all --graph --decorate'
alias gs='git status'
Save the Gist as public.
Step 2 - Alias for Updating
Now, we need a simple alias so we can easily update our aliases from Gist.
Get the raw URL for your .bash_aliases
Gist file (click the "Raw" button) and then add this to your .bash_aliases
Gist file:
alias alias_update_linux='wget -O ~/.bash_aliases <URL> && source ~/.bash_aliases'
⚠ Remember to replace the <URL>
with the raw URL.
This means when you run the command alias_update_linux
it will set the newest aliases from Gist.
The reason it's called _linux
in the end is because the syntax is different for Windows (we'll get to this later).
Step 3 - Gist Version (optional)
I've also added an alias, which contains the version of the Gist, but this is of course optional and can also be seen by the list of aliases. However, Gist uses cache so if you want, make sure you have all the aliases you can add this to your Gist file:
alias alias_version='echo version 1.0'
Remember to bump this version every time you edit your Gist file. Then you can run alias_version
and it will show the latest version fetched from Gist, so you know if you are up to date.
Step 4 - Setting Up Linux Servers
First Time Setup
Run this command to overwrite your current ~/.bash_aliases
file, and load the aliases.
wget -O ~/.bash_aliases <GIST-RAW-URL> && source ~/.bash_aliases
Remember to replace <GIST-RAW-URL>
.
If you type alias
now, you should see a list of the aliases from your Gist file, and if you added the version alias, you can see which version you are using.
Auto Sync on SSH Login (Linux)
Unless you want to manually run alias_update
on every ssh login on all servers, you can automatically run the command every time you login via ssh, so you're always up to date.
Edit ~/.bashrc
and add this to the bottom of the file:
alias_update
Now you always have the latest aliases when you connect to your server.
You might think this is going to slow down SSH logins, but in my experience, it's unnoticeable because Gist is so fast: ~0.030s. Try it yourself: time alias_update
.
Step 5 - Windows Setup
On Windows, I'm always using Cmder as my terminal. PHP and Composer is required for this to work.
In Cmder you can add new aliases using alias
, and they will still be available after a reboot, but they won't be synced up to the Gist file.
Make sure Composer's directory for vendor binaries is set in your PATH, for me it's located at %APPDATA%/Composer/vendor/bin
.
Run this to install setaliases:
composer global require xy2z/setaliases
Then, run this command in Cmder to automatically run "setaliases" every time Cmder starts.
echo setaliases <GIST-RAW-URL> >> %CMDER_ROOT%/config/user_profile.cmd
Restart Cmder, and your aliases should now be available, and updated on each new instance.
If you want, you can then go back to edit your Gist file, and add this alias for manually updating windows aliases:
alias alias_update_windows='setaliases <GIST-RAW-URL>'
Why PHP?
Windows (Cmder) and Linux don't use the same alias syntax, and stuff like 'sudo' doesn't exist in Windows, so the aliases needs to be formatted before it will work as expected in Cmder/Windows.
PHP and Composer makes it easy to install, update and remove the "setaliases" script, and since I'm a php developer, I always have those installed.
Tips
- To list all your aliases, and see the current version, run
alias
- Remember that aliases has tab support, so if you have many custom aliases you can prefix them. If you prefix your aliases with "my" you can type "my" and hit tab twice, you'll see a list of all your prefixed aliases.
- Watch for new releases on the setaliases repo, for future bug fixes, etc.
- Can't remember the alias? Search using
alias | grep <keyword>
- If you added the
alias_version
alias, you can run it after the update command, so you can see current alias version on each update.
If you have any alias tips feel free to post them in the comments.