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.
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.
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.
- 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
~/.bashrcfile, if it exists.
- macOS: This has not been tested on macOS, but it seems
wgetis not default installed. Instead, you can use
curl -O(or install wget) - just know some Linux servers may not have
curlinstalled. I choose
wgetbecause all Ubuntu servers has this by default, and in my testing
wgetwas a slightly faster than
Step 1 - Creating Your Aliases
Create a new Gist and name the filename
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
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.
~/.bashrc and add this to the bottom of the file:
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:
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
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>'
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.
- To list all your aliases, and see the current version, run
- 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_versionalias, 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.