Symlink dotfiles
Asked Answered
R

4

22

I am having trouble symlinking dotfiles. I have a folder in my home directory ~/dotfiles which I have synced to a github repo. I am trying to take my .vimrc file in ~/dotfiles/.vimrc and create a symbolic link to put it at ~/.vimrc. To do this I type in

ln -s ~/dotfiles/.vimrc ~/.vimrc

But when I run that it says

ln: /Users/me/.vimrc: File exists

What am I doing wrong?

Rental answered 2/10, 2017 at 21:55 Comment(1)
protip: In newer versions of Vim, you can also store your vimrc inside of your ~/vim directory for easier storage. This makes the path: ~/.vim/vimrc. See :h vimrcRaymer
D
26

That error message means that you already have a file at ~/.vimrc, which ln is refusing to overwrite. Either delete the ~/.vimrc and run ln again or let ln delete it for you by passing the -f option:

ln -s -f ~/dotfiles/.vimrc ~/.vimrc
Donkey answered 2/10, 2017 at 21:57 Comment(0)
S
13

There is a better solution for managing dotfiles without using symlinks or any other tool, just a git repo initialized with --bare.

A bare repository is special in a way that they omit working directory, so you can create your repo anywhere and set the --work-tree=$HOME then you don't need to do any work to maintain it.

Approach

first thing to do is, create a bare repo

git init --bare $HOME/.dotfiles

To use this bare repo, you need to specify --git-dir=$HOME/.dotfiles/ and --work-tree=$HOME, better is to create an alias

alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME

At this point, all your configuration files are being tracked, and you can easily use the newly registered dotfiles command to manage the repository, ex :-

# to check the status of the tracked and untracked files 
dotfiles status

# to add a file 
dotfiles commit .tmux.conf -m ".tmux.conf added"

# push new files or changes to the github
dotfiles push origin main

I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.

Sympetalous answered 27/10, 2020 at 5:40 Comment(0)
M
3

How to symlink all dotfiles in a directory recursively

  1. Have a dotfiles directory that is structured as to how they should be structured at $HOME

    dotfiles_home=~/dotfiles/home  # for example
    
  2. cp -rsf "$dotfiles_home"/. ~

    • -r: Recursive, create the necessary directory for each file
    • -s: Create symlinks instead of copying
    • -f: Overwrite existing files (previously created symlinks, default .bashrc, etc)
    • /.: Make sure cp "copy" the contents of home instead of the home directory itself.
Tips

Just like ln, if you want no headache or drama, use an absolute path for the first argument like the example above.

Note

This only works with GNU cp (preinstalled in Ubuntu), not POSIX cp. Check your man cp, you can install GNU coreutils if needed.

Thanks

To this and this.

Maidie answered 9/5, 2022 at 9:35 Comment(0)
E
0

Use GNU Stow for symlink your dotfiles.

Embracery answered 25/10, 2023 at 15:59 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Crevice

© 2022 - 2024 — McMap. All rights reserved.