跳转至内容

点文件 (dotfiles)

来自 ArchWiki

用户特定的应用程序配置传统上存储在所谓的 dotfiles(文件名以点开头的文件)中。通常的做法是使用 版本控制系统(例如 Git)来追踪 dotfiles,以便记录更改并在多台主机间同步这些文件。管理 dotfiles 有多种方法(例如直接在主目录中追踪 dotfiles,或者将其存储在子目录中,并通过 shell 脚本或 专用工具 进行符号链接/复制/生成)。除了介绍如何管理 dotfiles 外,本文还包含 Arch Linux 用户的 dotfiles 仓库列表

直接使用 Git 追踪 dotfiles

直接使用 Git 追踪 dotfiles 的好处是只需要 Git,且不涉及符号链接。缺点是 主机特定的配置 通常需要将更改合并到多个 分支 中。

实现此方法最简单的方式是在您的主目录中直接初始化一个 Git 仓库,并使用 gitignore(5) 模式 * 默认忽略所有文件。然而,这种方法有两个缺点:当您的主目录中有其他 Git 仓库时,可能会造成混淆(例如,如果您忘记初始化仓库,就会意外地操作到您的 dotfiles 仓库),并且您无法再轻松查看当前目录中哪些文件未被追踪(因为它们被忽略了)。

另一种没有这些缺点的替代方法是 Ask Hacker News: What do you use to manage your dotfiles? 中推广的“裸仓库和别名法”,只需三个命令即可设置:

$ git init --bare ~/.dotfiles
$ alias dotfiles='/usr/bin/git --git-dir="$HOME/.dotfiles/" --work-tree="$HOME"'
$ dotfiles config status.showUntrackedFiles no
注意 通常个人的 dotfiles 具有默认权限,但如果必须为某些文件设置特定的文件权限,则应使用其他方法,因为 git 不会存储权限(讨论)。

您的 dotfiles 可以在新系统上通过以下方式复制:

$ git clone --bare <git-repo-url> $HOME/.dotfiles
$ alias dotfiles='/usr/bin/git --git-dir="$HOME/.dotfiles/" --work-tree="$HOME"'
$ dotfiles checkout
$ dotfiles config --local status.showUntrackedFiles no
  • 若要在没有远程仓库的情况下执行此操作,可以使用 git bundles。要在源计算机上创建名为 dotfiles.bundle 的 git 包,请运行:
$ dotfiles bundle create --progress dotfiles.bundle --all
包的使用方式类似于 <git-repo-url>
$ git clone --bare dotfiles.bundle $HOME/.dotfiles


  • 如果您已经有一些可能会被覆盖的现有 dotfiles,则会遇到类似于以下内容的错误:
$ dotfiles checkout
error: The following untracked working tree files would be overwritten by checkout:
    .bashrc
    .gitignore
Please move or remove them before you can switch branches.
Aborting
您可以使用 $ dotfiles checkout -f 来重写现有文件,或者采用更安全的方法,使用以下脚本备份所有文件,然后再使用 checkout
mkdir -p .dotfiles-backup && \
dotfiles checkout 2>&1 | egrep "\s+\." | awk {'print $1'} | \
xargs -I{} mv {} .dotfiles-backup/{}

然后,您可以使用创建的 别名 来管理您的 dotfiles。如果您正在使用 Bash 并希望为该别名启用 bash 补全,只需安装 bash-complete-aliasAUR,然后将该别名及以下行添加到您的 ~/.bashrc 中。

$ complete -F _complete_alias dotfiles

在 bash 中实现补全的另一种方法是将以下内容添加到您的 ~/.bashrc 中(摘自 [1]):

source /usr/share/bash-completion/completions/git
__git_complete dotfiles __git_main
提示 为避免意外提交机密信息,请参阅 Git#过滤机密信息

主机特定的配置

在多台机器间同步 dotfiles 的一个常见问题是主机特定的配置。

使用 Git,可以通过为所有共享配置维护一个主分支(master branch),同时每台机器检出各自的特定分支来解决此问题。主机特定的配置可以提交到该机器特定的分支中;当主分支中的共享配置发生修改时,需要将各个机器分支基于更新后的主分支进行变基(rebase)。

shell 配置文件 等配置脚本中,可以使用条件逻辑。例如,Bash 脚本(如 .bashrc)可以根据机器名称(或类型、自定义变量等)应用不同的配置。

if [[ "$(hostname)" == "archlaptop" ]]; then
    # laptop specific commands here
else
    # desktop or server machine commands
fi

类似的功能也可以通过 .Xresources 实现。[2]

如果您觉得变基 Git 分支太麻烦,您可能希望使用支持文件分组工具,或者如果需要更大的灵活性,可以使用支持处理(processing)的工具。

工具

文件分组
如何将配置文件分组为配置组(也称为配置文件或包)。
Processing
一些工具会对配置文件进行处理,以便根据主机进行自定义。
名称 软件包 (Package) 编写语言 文件分组 Processing
dotbot dotbotAUR Python 配置文件
chezmoi chezmoi Go 基于目录 Go 模板
dot-templater dot-templater-gitAUR Rust 基于目录 自定义语法
toml-bombadil toml-bombadil Rust 配置文件 tera
dotdrop dotdropAUR Python 配置文件 Jinja2
点文件 (dotfiles) dotfilesAUR Python
dotter dotter-rsAUR Rust 配置文件 Handlebars
dt-cli dt-cliAUR Rust 配置文件 Handlebars
GNU Stow stow Perl 基于目录[3]
Mackup mackupAUR Python 每个应用程序自动同步
mir.qualia mir.qualiaAUR Python 自定义块
rcm rcmAUR Shell 基于目录(按主机或标签)
yas-bdsm Shell 基于目录
dotbackup dotbackupAUR Rust 配置文件 Shell 脚本
Nix home-manager Nix 灵活(使用 Nix 语言配置) 灵活(使用 Nix 语言配置)

封装 Git 的工具

如果您对 Git 不太熟悉,可能希望使用这些工具中的一种,它们(或多或少地)抽象化了版本控制系统。

名称 软件包 (Package) 编写语言 文件分组 Processing
dotbare dotbareAUR Shell (fzf) 仓库级别
dotgit dotgitAUR Python 文件名级别
homeshick homeshick-gitAUR Bash 仓库级别
homesick Ruby 仓库级别
Pearl pearl-gitAUR Python 仓库级别
vcsh vcsh Shell 仓库级别
yadm(1) yadm Shell 文件名级别
(按类/操作系统/发行版/主机名/用户)[4]
内置模板/Jinja2/ESH[5]
(可选)
dfm dfmAUR Perl 仓库级别
  1. 支持使用 GPG 或 OpenSSL 对机密文件进行加密。[6]

用户仓库

注意 此表格用作 dotfiles 的参考/示例;如果您要将您的 dotfiles 提交到此表格,请确保它们保持整洁、带有注释且为最新版本。
警告 这些 dotfiles 未经任何 Arch Linux 成员验证,请自行承担风险。
作者 Shell (Shell 框架) WM / DE 编辑器 Terminal 复用器 音频 监控 邮件 IRC 文件管理器 RSS 阅读器
adamperkowski Zsh dwm Neovim st tmux mpv 自定义 WeeChat
ananthu Zsh bspwm Neovim Alacritty mpv htop, Polybar neomutt WeeChat ranger
ayekat Zsh karuiwm Vim rxvt-unicode tmux ncmpcpp / mpd karuibar mutt Irssi
bachoseven Zsh dwm Neovim st tmux ncmpcpp bottom neomutt WeeChat Lf Newsboat
bamos Zsh i3 / xmonad Vim / Emacs rxvt-unicode tmux mpv / cmus conky / xmobar mutt ERC
brisbin33 zsh xmonad vim rxvt-unicode GNU Screen dzen mutt irssi
christian-heusel Zsh i3 Neovim st / Terminator byobu / tmux htop neomutt / Thunderbird WeeChat Nemo / ranger
CuterThanYou Zsh i3 / Hyprland Neovim Alacritty / Foot mpv Polybar / Yambar Lf / Thunar Newsboat
dikiaap Zsh i3-gaps Neovim Alacritty tmux i3blocks nnn
Earnestly Zsh i3 / orbment Vim / Emacs termite tmux mpd conky mutt WeeChat
egnrse Zsh / Bash Hyprland Neovim Alacritty VLC bottom / mission-center Dolphin
ErikBjare Zsh xmonad / Xfce4 Vim Terminator tmux xfce4-panel WeeChat
erikw Zsh / Bash dwm / macOS Neovim urxvtc tmux mpd mutt Irssi
filiparag fish bspwm Vim Alacritty tmux mpv / playerctl htop, Polybar mail-notification PCManFM
Freed-Wu Zsh openbox Neovim wezterm tmux cmus bottom neomutt WeeChat Neovim Newsboat
graysky Zsh Xfce Vim terminal ncmpcpp 自定义 Thunderbird
insanum Bash Herbstluftwm Vim evilvte tmux dzen mutt-kz
isti115 pwsh sway neovim alacritty tmux mpv / playerctl waybar / htop / ytop ranger
itsme Zsh Niri Helix kitty tmux mpv Waybar yazi
jasonwryan Bash / Zsh dwm Vim rxvt-unicode tmux ncmpcpp 自定义 mutt Irssi
jdevlieghere Zsh xmonad Vim terminal tmux htop mutt WeeChat
jelly Zsh i3 Vim termite tmux ncmpcpp mutt-kz-git WeeChat
jl2 Zsh swayfx geany foot mission-center / eww (栏) srain Thunar
karras Zsh Wayfire Neovim Terminator Waybar
MarkusZoppelt Zsh GNOME Neovim Alacritty tmux
maximbaz Zsh Sway kakoune kitty Waybar neomutt nnn
neersighted fish i3 Neovim Alacritty tmux ncmpcpp
nimaipatel fish awesome Neovim Alacritty ncmpcpp
oibind fish awesome Neovim st tmux htop-vim WeeChat Lf
orhun Bash i3-gaps Neovim Alacritty i3status WeeChat tere
patri9ck Zsh bspwm Vim kitty Thunar
peterzuger Zsh i3-gaps Emacs rxvt-unicode GNU Screen MOC htop
polyzen Zsh i3 Neovim Alacritty tmux mpv i3status / htop himalaya ranger Newsboat
potamides Bash awesome Neovim termite tmux ncmpcpp conky / htop mutt WeeChat ranger
sistematico Zsh / fish / Bash i3-gaps Vim / nano termite tmux ncmpcpp Polybar mutt WeeChat
thecashewtrader Eshell EXWM Emacs Emacs (VTerm) Emacs Bongo htop mu4e ERC Dired Elfeed
thiagowfx Bash / Zsh i3 Vim Alacritty tmux playerctl i3status ranger
tplasdio bash (ble.sh) awesome neovim alacritty tmux mpv, mpvs htop neomutt WeeChat lf
tuurep Zsh openbox Neovim Alacritty Polybar
unrealapex Zsh dwm Neovim st ncmpcpp htop neomutt Irssi fff Newsboat
w0ng Zsh dwm Vim rxvt-unicode tmux ncmpcpp 自定义 mutt Irssi
whitelynx fish i3 Neovim kitty i3pystatus
whynothugo Zsh Sway Neovim Alacritty mpv Waybar / top neomutt Nemo

参见

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.