Dotfiles – What is a Dotfile, Why do we need Dotfile and How to create it in Mac

Dotfile manager (personal sh script) to automate and ease the hustle of setting up or switching to new mac. No more panic attacks on mac crash. ✌️

Dotfiles – What is a Dotfile, Why do we need Dotfile and How to create it in Mac

Introduction

Let’s learn from my situation. Within a year I have installed macOS 6 times for various reasons (macOS crashes, switching to new mac, clean install every half year). It is frustrating to setup your development environment from scratch. And it takes time to get back to your productivity level.

Once my ex manager Ruben was giving me some productivity tips, he showed me his git aliases. And explained how it can save time in long run. Recently my macOS crashed and I lost all my aliases and dotfile configurations. As you get used to with your personal configurations it becomes hard to quickly get back to the same efficiency. By the time iCloud is not handy to store and sync such things. Fortunately, I setup most of my configurations from Ruben’s repository. So I went back to his repository and copied again. The loss was less. But there I realised the importance of saving my own dotfiles on GitHub.

What the heck is dotfile

In unix world, most programs are configured via command line. These programs usually save their configurations in user’s home directory in plain text file or directory. Dotfiles named that way because each file or directory starts with (.) dot. These dotfiles are hidden by the operating system by default.

Why to use dotfiles

Dotfiles are personal to you. Developers who work in terminal, spend a sufficient amount to time tweaking and optimising settings that best suit their workflow and preferences. The end goal of such settings is to make life easier and more productive.

Problems

With great power comes great responsibility. As pointed earlier, if you lose your personal settings you lose your productivity. Saving files to cloud can be a solution for it. But how will you remember where to place which dotfile. Mostly dotfiles are placed at home directory but this is not true for each case. Here your power of being a programer comes in handy. Simply write a script and automate the file positioning and replacing. Final problem; how to sync development environment across multiple machines. Solution is to create a single source of truth. While scripting file positioning, instead of hard replacing, simply place symbolic link files. So your files on cloud will become source of truth. So now we have a script and some files to store why not using Github as cloud service?

How to automate

Create a directory for single source of truth.

cd ~
mkdir toast-mac
cd toast-mac
mkdir dotfiles
cd dotfiles

Move your dotfiles to that directory. i.e. .gitconfig and .zshrc

mv ~/.zshrc ~/toast-mac/dotfiles/
mv ~/.gitconfig ~/toast-mac/dotfiles/

If they do not already exist, you can create them.

touch ~/toast-mac/dotfiles/.zshrc  ~/toast-mac/dotfiles/.gitconfig

Now that you have moved your, dotfiles to a single location. Place symbolic link from where you have moved them. Since we have moved them from $Home .

ln -sfv ~/toast-mac/dotfiles/.gitconfig ~
ln -sfv ~/toast-mac/dotfiles/.zshrc ~

You can create the .sh to store your settings.

touch install.sh

Open file in your favourite editor.

open install.sh

Or since I use vscode so,

code .

Next time just run the the file and it will execute the changes.

./install.sh

macOS defaults

macOS and apps keep their settings in .plist files and it can be modified via command line. Following are some settings that I prefer,

# Finder: show hidden files by default
defaults write com.apple.finder AppleShowAllFiles -bool true
# Dock: move dock to the left side of screen
defaults write com.apple.dock orientation left
#- Screencapture: Save screenshots to the desktop
defaults write com.apple.screencapture location -string "$HOME/Desktop"
#- Screencapture: Save screenshots in PNG format"
defaults write com.apple.screencapture type -string "png"

You can find more settings from here: https://github.com/mathiasbynens/dotfiles/blob/main/.macos

Store them in your install.sh file. So next time when you run, it will overwrite your macOS preferences.

Homebrew

Homebrew is essential when you have macOS.

which -s brew
if [[ $? != 0 ]] ; then
    # Install Homebrew
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
    brew update
fi

Save to Cloud

Create a repository on github and push the files there

cd ~/toast-mac
git init
git add .
git commit -m "Added dotfiles"
git remote add origin <url>

where <url>, the GitHub url of the repository you previously created

git push -u origin main

Conclusion

Hope I have shown you enough in order to save and sync your own dotfiles. Like I said dotfiles are personal to everyone. Right development environment sets with lots of trials and error. Your dotfiles will grow as you grow in your career, and your preferences will change with time. But next time when your mac crashes, you will not panic 😅 like me at least. Because you have your dotfile backup on Github.

Attaching link to my dotfiles for inspiration: https://github.com/hashirshoaeb/toast-mac

Thanks for reading!