Dotfiles Essentials: Enhance Your Command Line Experience
"Dotfiles" is general description referring to the configuration files that determine settings and preferences inside your terminal emulator.
Configuration files help you customize your terminal and allow you to install plugins or other software.
The benefits of plugins or terminal software can have a variety of benefits:
Heighten what you can do or make certain things easier.
Add custom fonts or auto-complete.
Create program aliases, have programs run on start-up.
etc.
The reason they are called "dotfiles" comes from how configuration folders or files have a period preceding the name (".bashrc"). These files are hidden by default, but are visible from the terminal.
Why should you care?
If you're reading this article, chances are you already know your way around a terminal and have heard of dotfiles. But why should you care about managing them?
Dotfile management solves the problem of not having a way to backup your terminal settings and transferring your settings to a new computer.
Imagine having all your custom aliases, plugins, and settings ready to go with just a few commands. The speed of setting up a new system boosts productivity, ensures consistency across different systems, and give you the ability to back-up your config files.
This command will output the file location on your computer for your home, and where most dotfiles are located, or inside $HOME/.config
(hidden hence the "dot").
echo "$HOME"
Then type ls -a
to display all files in your $HOME
directory (even the hidden files)
If the command above did not work on your system; you can use my personal reference to figure out how to find your terminal: how to find your terminal.
Google can take you the rest of the way there.
My Experience
Teaching yourself programming and computer science is an adventure filled with diverse experiences, topics, and community.
Terminal configuration is a great way to build your skills and knowledge of the terminal (for a new career or a growing passion).
If you want to feel like a hacker in the terminal, I recommend learning how to use vim (in 100 seconds).
Learning vim was a great experience, not just for productivity but also for challenging myself to master something difficult and new through slow and steady practice.
If you strive for perfection, you might fail, and that’s okay; it’s part of the process.
The acceptance of failure and learning from mistakes helped me grow in other parts of my life and encouraged me to do things that I am “bad” at, and to not be afraid of feeling embarrassed or stupid.
Learning difficult things eventually lead me down the rabbit hole of customizing my NeoVim configuration.
Editing your .vimrc
is the first step in becoming a power-user and that’s where the rabbit hole begins.
My Configuration Repository
This command output shows the current file structure for my .dotfiles
folder.
.
├── README.md
├── install.json
├── install.sh
├── macos
│ ├── complete-vimtutor
│ ├── datetime
│ │ └── datetime.sh
│ ├── nvim
│ ├── scripts
│ │ └── cht.sh
│ ├── tmux
│ ├── vim
│ └── zsh
├── raycast
├── ubuntu
│ ├── README.md
│ ├── datetime
│ │ └── datetime.sh
│ ├── nvim
│ ├── scripts
│ │ └── cht.sh
│ ├── starship
│ ├── tmux
│ ├── vim
│ └── zsh
└── uninstall.sh
17 directories, 10 files
My .dotfiles
folder is where I store (and backup) my configuration files on my computer.
Most terminal tools will store their settings files inside .config
in your $HOME
directory but there is a way around this.
The overall structure of the folder contents above is:
README.md.
install.json.
install.sh.
uninstall.sh.
macos folder.
ubuntu folder.
I only need to structure my dotfiles folder a certain way with two scripts for installing and uninstalling these dotfiles to my $HOME
directory on my computer.
In that case, I can store my files in an organized Github repository and be able to backup or clone those files to other machines.
The scripts work is by utilizing a program called stow
. Stow helps you manage symbolic links on your computer.
What are Symbolic Links?
A symbolic link is almost like desktop “shortcut” but in your terminal.
Symbolic links exist only to “point” to another file’s location on your computer.
Let's say you have a file located at /home/user/documents/file.txt
, and you want to create a symbolic link to that file in another directory, such as /home/user/Desktop/
.
The command would be:
ln -s /home/user/documents/file.txt /home/user/Desktop/link_to_file.txt
Breakdown:
ln -s
creates a symbolic link./home/user/documents/file.txt
is the target file (the file you want to link to)./home/user/Desktop/link_to_file.txt
is the name and location of the symbolic link you are creating.The structure is
ln -s <target> <location/name of link>
After running the command, you’ll have a symbolic link called link_to_file.txt
on your Desktop, pointing to the original file.txt
.
Why do you need programs like Stow
to manage your Symlinks?
Symbolic links can be tricky to manage for several reasons:
Broken Links: If the target file or directory of a symbolic link is deleted, moved, or renamed, the symbolic link becomes "broken" and points to nothing.
Complex Directory Structures: If you have many symbolic links scattered across your system, it can be difficult to keep track of where the links are pointing, especially in large projects with complex directory structures.
Manual Creation: Symbolic links must be created manually for each file or directory. This can be error-prone if you have a large number of files to manage and link.
File Overwriting: If you're managing dotfiles (configuration files) or scripts using symbolic links, manually linking and unlinking files could accidentally overwrite existing files, leading to data loss or inconsistent system states.
How Does stow
Help?
stow
is a symlink management tool that simplifies the process of creating and managing symbolic links, especially for configuration files (e.g., dotfiles). Here's how it helps:
Automated Link Creation: Instead of manually running
ln -s
for each file,stow
allows you to create links for entire directories in one command. This is particularly helpful when you have a directory of dotfiles (e.g.,~/.dotfiles
) that you want to symlink into your home directory.Example:
stow directory_name
This command will automatically create symlinks for all files in
directory_name
into your current directory.Namespace Separation:
stow
operates by using a directory structure to keep files in "packages" (subdirectories). This allows you to manage different configurations for different systems or tools separately. For example, you might havemacos/
andubuntu/
directories in your dotfiles repository, each containing the appropriate config files for those environments.Easily Reversible:
stow
can automatically remove the symlinks it creates with an "unstow" command, effectively making the process reversible. This prevents issues of manual symlink removal or accidentally leaving behind broken links.Example:
stow -D directory_name
This will delete all the symbolic links created from
directory_name
.Avoids File Overwrites:
stow
is designed to prevent overwriting files. If it detects that a file already exists in the target location, it will not create the symbolic link, helping avoid accidental file overwriting.Cross-System Compatibility: If you're using
stow
to manage dotfiles for different systems (e.g., macOS vs. Linux), you can easily switch between configurations by stowing the appropriate directory for each system. This helps with portability, as you can move your dotfiles repository across systems without worrying about broken symbolic links.
Summary of stow
and Symlinks
Symbolic links are like desktop shortcuts, pointing to another file's location.
Challenges in managing symbolic links:
Broken links if the target file is altered.
Complex directory structures complicate tracking.
Manual creation is error-prone.
Risk of file overwriting.
stow
tool benefits:Automates link creation for directories.
Maintains separate namespaces for configurations.
Easily reversible with "unstow" command.
Prevents file overwrites.
Supports cross-system compatibility for dotfiles.
The script (largely inspired by GeekMasher) is a simple way we can make sure our files are pointing to our $HOME
with stow
and that there are no broken connections.
Breakdown of install.sh
This script is pretty straight forward but I am going to breakdown the overall logic and show you the code.
Steps
Detect Operating System (Linux, Mac, etc.)
if linux: use ubuntu dotfiles folder
if mac: use mac dotfiles folder
verifies that the specific folder exists
Remove all
stow
symlinks that existre-add all symlinks with stow based on what is inside your dotfiles folder.
The bash script described above is listed below, but if you want to see all my files that I use in my terminal on Mac or Ubuntu; You can check it out here:
#!/bin/bash
set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PRESENT=false
# Detect the operating system
OS=$(uname)
# Define DOT_FOLDERS based on the OS
if [[ "$OS" == "Linux" ]]; then
DOT_FOLDERS_DIR="$SCRIPT_DIR/ubuntu"
elif [[ "$OS" == "Darwin" ]]; then
DOT_FOLDERS_DIR="$SCRIPT_DIR/macos"
else
echo "Unsupported operating system: $OS"
exit 1
fi
echo "[+] Dotfiles :: $DOT_FOLDERS_DIR"
if ! command -v stow &> /dev/null; then
echo "stow is not installed, installing now"
sudo apt install -y stow
fi
# Check if the directory exists
if [[ ! -d "$DOT_FOLDERS_DIR" ]]; then
echo "Directory $DOT_FOLDERS_DIR does not exist."
exit 1
fi
# Stow and unstow relevant directories based on the OS
for folder in "$DOT_FOLDERS_DIR"/*; do
if [[ -d "$folder" ]]; then
folder_name=$(basename "$folder")
echo "[+] Folder :: $folder_name"
stow -d "$DOT_FOLDERS_DIR" -t $HOME -D $folder_name \
--ignore=README.md --ignore=LICENSE
stow -d "$DOT_FOLDERS_DIR" -t $HOME $folder_name
fi
done
# Look for DOT_FOLDER in the .dotfiles
while IFS= read -r var; do
[[ $var =~ ^DOT_FOLDER.* ]] && PRESENT=true
done < "$HOME/.dotfiles"
# If not present, write the DOT_FOLDER var into the file
if [[ $PRESENT == "false" ]]; then
echo "[+] Adding DOT_FOLDER to .dotfiles"
echo "DOT_FOLDER=$SCRIPT_DIR" >> $HOME/.dotfiles
fi
# Reload shell once installed
echo "[+] Reloading shell..."
exec $SHELL -l
Conclusion
Dotfiles are configuration files that help customize your terminal settings, allowing for efficient setup, plugin installation, and a consistent experience across devices.
Managing dotfiles with tools like stow
simplifies the setup process, automates the creation and removal of symbolic links, prevents file overwrites, and ensures compatibility across different systems.
This guide delves into the benefits of dotfiles, symbolic link management, and provides a step-by-step breakdown of using a stow-based bash script to manage your configuration files.
If you have any questions or concerns, you can reach me on my Twitter.
If you're interested in subscribing to my Substack newsletter, where I write about tech and share new resources I discover, please enter your email below.
I write blog posts and other content in a newsletter format. I share tech resources, blog posts I like, and new notes I create in my digital garden.
Thanks!
Subscribe to my newsletter
Read articles from Diego Guisande directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by