Command Line Aliases
_ _ _
/ \ | (_) __ _ ___ ___ ___
/ _ \ | | |/ _` / __|/ _ \/ __|
/ ___ \| | | (_| \__ \ __/\__ \
/_/ \_\_|_|\__,_|___/\___||___/
I tend to use aliases on the command line fairly sparingly but I've built up a useful collection of things that really save me time in my daily workflows. I've found myself giving a few of these away recently and somewhat surprised that some really good devs I've been working with are doing a few things more manually than they need to. Hence, these are my notes on speeding up your daily workflow(s) with some aliased commands.
One of the really neat things I like about aliases, and my main use case, is the fact that you can come up with a command line that's relatively complicated (something you probably wouldn't remember on a daily basis, probably because you only run the command occasionally) and squirrel it away in one of your profile files for reuse.
Logging into stuff
A pattern for aliases that's really useful is the ability to log into various services using passwords, keys or tokens on the command line. This is often a bad idea on a shared system and even worse if you go storing your secrets in your profile files. Therefore, the first thing to do when using this pattern is to establish how to get secrets from your secret store on the command line.
I use 1Password, we get it provided through work, but it's one of those tools that I would probably pay for privately if I didn't have it. I was formerly a lastpass user and I find 1Password much better. It has a command line tool called "op" that allows you to grab passwords onto the command line using graphical authentication in your desktop. It looks something like this:
$ op read "op://Private/Some Service/credential"
You can then pipe the output of this into another command. Such as this example to login to a container registry:
$ echo $(op read "op://Private/Container Registry/key") | docker login -u iamapikey --password-stdin https://icr.io
If you need to re-use the same key you can create a variable for the key name, so the above example might change to something like this:
$ CONTAINER_REGISTRY_KEY_REF="op://Private/Container Registry/key"
$ echo $(op read "$CONTAINER_REGISTRY_KEY_REF") | docker login -u iamapikey --password-stdin https://icr.io
The above is just scratching the surface and can be applied to all sorts of things that you might want to log into such as other cloud services, code repositories or making VPN connections.
Privilege Escalation
Many commands need to be run as the root user, in these cases they are often preceded by the "sudo" command. For these instances, I often find it useful to create an alias of the command with sudo already in place e.g.:
$ alias dnf="sudo dnf"
$ alias systemctl="sudo -E systemctl"
Change Default Behaviour
Some commands do just want you want but you find you want to regularly (or always) alter their behaviour. For example, I generally find myself coding in TypeScript/JavaScript or Python at the moment. If I want to search my source code repositories I generally still use grep rather than the text search in an IDE such as vscode. In these instances, I'm pretty much never interested in searching through my node_modules or virtual environments. Hence, aliasing grep like this:
$ alias grep='grep --color=auto --exclude-dir=node_modules --exclude-dir=po --exclude-dir=env --exclude-dir=venv --exclude-dir=.venv'
Here's one that I find makes journalctl output a bit more readable for a single host:
$ alias journalctl="journalctl --no-hostname"
Complicated Commands
You may develop some complicated commands that you occasionally want to re-use but would need to re-construct the command from scratch next time around because it'll be long enough between now and when you next need to run it that you'd forget how to run it to your liking. Some examples:
Have a remote desktop appear at a size, language and with other preferences:
$ alias rdesktop="rdesktop -g 1440x900 -k en-gb -a 16 -r clipboard:CLIPBOARD"
Run the less command with syntax highlighting:
$ alias less-syntax-highlight="LESS=' -R --use-color ' LESSOPEN='| /usr/bin/src-hilite-lesspipe.sh %s' less"
Add my default git commit template to the repo I'm currently working on:
$ alias git-add-commit-tempate='git config --add commit.template ~/.gitmessage'
Rebuild the NVidia kernel module
$ alias rebuild-nvidia-kernel-module='akmodsbuild --kernels $(uname -r) /usr/src/akmods/nvidia-kmod.latest'
Shell Completion
This one doesn't really fit in with the rest of the blog post since it's not about an alias but it's still a useful workflow speed up. That is, to look out for shell completions that might be available for commands that you use regularly. This can add various "smarts" to the tab key in your shell such that the thing you were about to type is made much more accessible. Many commands these days come with a sub-command that will generate the completion configuration for you. These can be included into your shell with a template along the lines of this:
$ source <(command completions here)
A couple of examples for the bash shell:
$ source <(kubectl completion bash)
$ source <(pip3 --disable-pip-version-check completion --bash)
Comments