Syncing Aliases on Linux and Windows

October 9, 2020
Automatically sync your bash aliases on Linux and Windows (Cmder)

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.

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

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

If you have any alias tips feel free to post them in the comments.

Follow RSS/Atom Feed