Understanding the Machine-aware Bookmarks in the Textsmith Terminal
Ishe Chinyoka
In this post, I am going to talk of an interesting possibility for those who keep their configuration files in what are known as dotfiles. This usually happens when you work on more than one machine at a time but wishes to preserve your settings across those machines. Using git is an obvious choice for this as you can clone your dotifles on all the machines you work with.
However, I will not talk about the mechanics of setting up these dotfiles and a safe way to clone them between computers. Rather, I will be discussing how you can deal with bookmarks that depend on a particular platform.
The Problem
A key feature of the Textsmith terminal is the ability to bookmark commands. Such bookmarks may look more or less like aliases but they can be safely run in your terminal followed by an ALT-Enter to activate them.
But what happens when you bookmark a command that is platform-dependent? Take for example this update command on Ubuntu:
sudo apt update -y Such a command will not make sense on a Fedora machine where to update we need the sudo dnf update -y instead.
But I don’t want to type this command and want to take advantage of the bookmarking feature of the Textsmith.
This will mean that if you have three machines, you will always wrestle with how to tailor the bookmarks to suit them. But then you can’t keep such bookmarks in the dotfiles.
Conditional Bookmarks to the Rescue
The solution lies in what are known as conditional bookmarks. While every bookmark has a name followed by a colon and the commands to be run, a conditional bookmark uses a vector or a list to hold its commands.
Its structure is like this:
- It starts with a comment symbol (the
#mark); - Followed by the bookmark name with no space between the comment mark and the bookmark name;
- Then a colon (
:); - Then a list of machine names with their commands in quotes.
This list has each name of a machine that this bookmark applies to. To show that this is a machine name, we put a leading colon followed by the name of the machine.
After the machine name follows a space and then the commands for that machine quoted.
For example, let us create a bookmark called “update” which updates our three machines.
So the machine names we use here are the same as what is shown in the hostnamectl or what is in /etc/hostname.
I will give an example of my three machines–one running Archlinux (anvil), Linux Mint (bellows), and Fedora (crucible).
For my update bookmark, I type a string like this:
#update: [:anvil "sudo pacman -Syu --noconfirm", :bellows "sudo apt update -y && sudo apt upgrade -y", :crucible "sudo dnf update -y"]Then pressing Alt-M will create this new bookmark.
Running a conditional bookmark
To run a bookmark like the one we created above which is platform-specific and looks at the machine name, we just run it as any other command.
In this case, typing “update” then ALT-Enter will result in the Textsmith checking for the existence of this bookmark, and running only the commands for this machine.
If it happens that I am on a machine called “chisel” running Gentoo, the Textsmith will warn me that there is no update for chisel. I will have to update this bookmark if I need to continue using this bookmark.
Otherwise on a machine like anvil, crucible or bellows, it will just execute.
Conclusion
While general bookmarks can be fine as they are mainly dependent on the executables that we have, it may be challenging when it comes to commands that rely on the platform we have.
Take advantage of the Textsmith’s conditional bookmarking to have portable bookmarks in your dotfiles. Never struggle to hand-code your bookmarks file when all what you can do is place your machine in a conditional list and be done with it.
Thank you for reading this, until next time, have a pleasant bookmarking experience with the Textsmith terminal!