Introduction

This post is for Mac users.

Following my previous post, here's another small thing I noticed when I replaced my Mac and moved over to the new one.

I have a few homemade scripts (Python and so on) on my Mac that I use for work, and I had registered them as aliases so I could run them right away from the terminal. While working on my new Mac, I tried to run one of those scripts as usual, but when I typed the command, nothing worked.

On my old Mac, I could run them just by opening the terminal and typing a short command. Then I remembered, "Oh right, I set something up for that!" But I couldn't remember what exactly I had done. Or rather, after setting it up, I had been using it as if it were just a normal part of the system, so I had forgotten that I had set it up myself in the first place.

And that made sense, because this time I didn't restore everything from my backup at once. Instead, I've been bringing back only what I need, bit by bit, so of course the settings I used to have weren't on the new Mac.

When I looked into it again, I found that I had been using something called shell aliases. So, partly as a review for myself, I'll write about that this time.

What Is an Alias?

An alias is a way to call a long command with a short name. Mac's Finder also has aliases, but what I'm talking about here is more like the terminal version of that, a shortcut for commands.

For example, to run my homemade script, I used to do this every time:

cd /Users/(username)/Documents/Github/myscript python3 main.py 

Once you set up an alias, this is all you need:

myscript 

Since the full path is written inside the alias, you can run it from any folder right after opening the terminal, which is really convenient.

You write the setting in a file called ~/.zshrc. This is the terminal's configuration file, and it's loaded automatically when you start the terminal. It looks like this:

alias myscript='cd /Users/(username)/Documents/Github/myscript && python3 main.py' 

The steps are:

  • Open the file with open ~/.zshrc
  • Add the alias at the end and save
  • Apply it with source ~/.zshrc

If you forget to run source, the change won't take effect right away, so be careful. Also, you can see a list of all registered aliases by typing alias. It's handy when you forget what you've set up, like I did.

I Restored It, but It Still Didn't Work

The .zshrc from my old Mac was still in my backup, so I brought it back to the new Mac. By the way, .zshrc is a hidden file, so you normally can't see it in Finder. You can show hidden files by pressing ⌘ + Shift + .

With that, my aliases were back, and the command itself started to go through. But then I got a different error:

ModuleNotFoundError: No module named 'yaml'

At first I thought I had set up the alias wrong, but that wasn't it. The cause was that the Python libraries the script uses weren't installed on the new Mac. On my old Mac, the ones I had installed long ago were still there, so I had never noticed.

I checked which libraries the script imports, reinstalled them with pip, and it finally worked.

Conclusion

I thought that once I restored the aliases, everything would be back to normal, but in reality, the environment those aliases depended on didn't come back with them. Restoring a configuration file is easy enough, but what it was quietly depending on only becomes clear once things stop working.

Like this time, when you try to do the same things you used to do after switching machines, you can trip over things you didn't even remember yourself.

If you're planning to get a new Mac, it might be a good idea to pay a little attention to your terminal settings, too.

Thanks for reading!