An attempt to use symlink to solve file syncing problem


I’m a Neovim user.

I maintain my Neovim configuration under ~/.config/nvim, as this is the default location where Neovim looks for its configuration files. However, I also store these configuration files (potentially with other configuration files like ~.zshrc) in a Git repository under ~/dotfiles/ (i.e. ~/dotfiles/nvim for Neovim config) for version control and syncing with GitHub.

The challenge is that I’ve been manually copying changes between those two directories, which quickly becomes tedious.

To make this process more efficient, I automate this syncing using symlinks, ensuring any updates are reflected in both location without any extra efforts.

Step 1: Remove the existing nvim directory under ~/.config/nvim

rm -rf ~/.config/nvim
ln -s ~/dotfiles/nvim ~/.config/nvim

To check if the symlink is created successfully, run in ~/.config/:

ls -l nvim

# expected output:

lrwxr-xr-x  1 papattaradaapithanangsiri  staff  46 21 Sep 20:54 nvim -> /Users/papattaradaapithanangsiri/dotfiles/nvim

This will ensure that any changes made in ~/.config/nvim are reflected in ~/dotfiles/nvim, vice versa. Now we can manage just one directory and can push to GitHub directly from both directories (i.e. both points to the same directory).

  • A symbolic link is essentially a pointer that contains the path to the original file or directory it references. When we access the symlink, the system redirects us to the target location. In this case, the symlink in ~/.config/nvim contains the path to ~/dotfiles/nvim

  • When a symlink is deleted, it only removes the link itself, not the original file or directory it points to. However, if the original file is deleted, the symlink will become a “broken link”, pointing to a location that no longer exists