Okay, let's learn how to use stow on Arch Linux. stow is a fantastic tool, especially popular among Linux users for managing dotfiles (configuration files) by creating symbolic links.
It's not specific to Arch Linux, but the installation method and the common use case (dotfiles on a highly configurable system like Arch) make it very relevant.
1. What is stow and Why Use It?
- What it is:
stow is a symlink farm manager. It takes directories (which it calls "packages") and creates symbolic links from a target directory (usually your home directory ~) back to the files within those package directories.
- Why Use It?
- Dotfile Management: This is the most common reason. You can keep your
~/.bashrc, ~/.vimrc, ~/.gitconfig, etc., in separate directories within a single, version-controlled repository (like a Git repo).
- Organization: Keeps related configuration files grouped together (e.g., all Bash configs in one folder, all Vim configs in another).
- Easy Deployment: On a new machine, clone your dotfiles repo and run
stow for each package to set up your configurations instantly.
- Easy Updates: Update your dotfiles in their source location, and the changes are reflected immediately because the files in
~ are just symlinks.
- Easy Removal: Don't want a specific configuration anymore?
stow -D removes the symlinks cleanly.
2. Installation on Arch Linux
stow is in the official repositories. Installation is straightforward using pacman:
sudo pacman -S stow
3. How stow Works: The Core Concept
stow relies on a specific directory structure and operates based on where you run the stow command from and the argument you give it.
- Stow Root Directory: This is the main directory where you store all your "packages". A common choice is
~/dotfiles. This directory is where you will run the stow command from.
- Package Directories: These are subdirectories within the stow root. Each subdirectory represents a set of files you want to link (a "package"). For dotfiles, you might have
bash, vim, git, etc.
- Target Directory: This is the directory where
stow will create the symbolic links. By default, this is the parent directory of the stow root. If your stow root is ~/dotfiles, the default target is ~. This is almost always what you want for dotfiles.
Example Structure (in ~/dotfiles/):
~/dotfiles/
├── bash/
│ └── .bashrc
├── vim/
│ ├── .vimrc
│ └── .vim/
│ └── plugins/
│ └── ...
└── git/
└── .gitconfig
When you run stow bash from within ~/dotfiles/:
stow looks inside ~/dotfiles/bash/.
- It finds
.bashrc.
- It creates a symlink in the target directory (
~) pointing back to the source file: ~/.bashrc -> ~/dotfiles/bash/.bashrc.
If the package directory has subdirectories (like vim/.vim), stow will create corresponding directories (if they don't exist) and symlinks in the target:
~/.vimrc -> ~/dotfiles/vim/.vimrc
~/.vim/ (created as a directory if needed)
~/.vim/plugins/ (created as a directory if needed)
~/.vim/plugins/... -> ~/dotfiles/vim/.vim/plugins/...
4. Basic stow Commands
Assuming your stow root is ~/dotfiles, and you are currently in that directory (cd ~/dotfiles):
-
Stow (Link) a Package:
stow bash
This will create symlinks for everything inside ~/dotfiles/bash/ in your home directory (~).
-
Unstow (Unlink) a Package:
stow -D bash
This will remove the symlinks that were created by stow bash (but not the original files in ~/dotfiles/bash/).
-
Restow (Update) a Package:
If you add or remove files within a package directory (e.g., add .bash_profile to ~/dotfiles/bash/), you can update the links:
stow -R bash
This is equivalent to stow -D bash && stow bash.
-
List Stowed Packages:
stow -L
Shows which packages from your current stow root have been stowed.
-
Simulate (Dry Run): EXTREMELY USEFUL! Always use the dry run option first to see what stow will do before it actually does it.
stow -n bash
This will show you exactly which symlinks would be created or removed without making any changes. -n is short for --no-execute.
5. Setting up Your Dotfiles with stow (A Common Workflow)
Let's walk through setting up your dotfiles with stow.
-
Create Your Stow Root:
mkdir ~/dotfiles
cd ~/dotfiles
-
Initialize Git (Optional but Recommended):
git init
# (Later: git add .gitignore, git commit, git remote add origin ..., git push)
-
Create Package Directories:
Make a directory for each set of configurations you want to manage.
mkdir bash vim git
-
Move Existing Config Files: This is a critical step. If you already have configuration files (like ~/.bashrc), you need to move them into their respective package directories before stowing. stow will not overwrite existing non-symlink files by default (it will error out, which is usually good!).
# Example: Move your existing .bashrc
mv ~/.bashrc ~/dotfiles/bash/
# Example: Move your existing .vimrc and potentially the .vim directory
mv ~/.vimrc ~/dotfiles/vim/
if [ -d ~/.vim ]; then
mv ~/.vim ~/dotfiles/vim/
fi
# Example: Move your existing .gitconfig
mv ~/.gitconfig ~/dotfiles/git/
Note: Repeat for all the config files you want to manage.
-
Stow Your Packages: Now, run stow for each package from within your ~/dotfiles directory.
# Always run dry-run first!
stow -n bash
stow -n vim
stow -n git
# If the dry-run looks good, run for real:
stow bash
stow vim
stow git
After running these commands, check your home directory (ls -la ~). You should see entries like:
lrwxrwxrwx ... user ... .bashrc -> dotfiles/bash/.bashrc
lrwxrwxrwx ... user ... .vimrc -> dotfiles/vim/.vimrc
lrwxrwxrwx ... user ... .gitconfig -> dotfiles/git/.gitconfig
drwxr-xr-x ... user ... .vim/ <-- If .vim directory was moved/stowed, this might be a symlink or a real directory containing symlinks, depending on stow version and content. Modern stow is smart about this.
-
Add to Git and Commit:
cd ~/dotfiles
git add .
git commit -m "Initial commit of my dotfiles"
# (If you have a remote repo: git push)
6. Important Notes and Tips
- Run from the Stow Root: Remember to always run
stow commands (stow bash, stow -D vim, etc.) from your stow root directory (~/dotfiles/ in our example).
- Conflicts: If you try to stow a package, and a file that
stow wants to link already exists in the target directory and is not a symlink, stow will stop with an error. This is a safety feature to prevent accidental data loss. The solution is usually to move the existing file into your package directory before stowing (as shown in step 4 above), or manually delete/move the conflicting file.
- Excluding Files: You can tell
stow to ignore certain files within a package using .stow-local-ignore or .stow-global-ignore files. This is useful for temporary files, backup files (*~), or files managed differently. See man stow for details.
- Target Directory (
-t): While the default target (../, which is ~ if stow root is ~/dotfiles) works for dotfiles, stow can link to any directory using the -t flag: stow -t /usr/local/stow foo. This is less common for personal dotfiles but part of stow's original purpose (managing installations in /usr/local).
- Unstowing Before Switching Branches/Cloning: If you use Git and switch branches that significantly change a package's files, it's often safer to
stow -D <package> before switching branches and then stow <package> again after switching, just to avoid potential link confusion.
7. Further Learning
- Man Page: The definitive source is the
stow manual page: man stow. It explains all the options (-D, -R, -n, -t, -S for --adopt, ignore files, etc.).
- Online Tutorials: Search for "stow dotfiles tutorial" or "manage dotfiles with stow". There are many guides, often specific to different shells or editors, that can give you more ideas.
stow is simple, powerful, and fits very well into a minimalist, command-line centric environment like Arch Linux. Start with one or two packages (bash and vim are good candidates), use the dry run (-n) religiously, and gradually expand to manage more of your configuration files. Good luck!