dotfiles

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dotfiles

Dotfiles

Manage and sync configuration files across machines.
实现多机器间配置文件的管理与同步。

GNU Stow (Simple)

GNU Stow(简易方案)

Setup

配置步骤

bash
undefined
bash
undefined

Install

安装

brew install stow # macOS apt install stow # Ubuntu
brew install stow # macOS系统 apt install stow # Ubuntu系统

Create dotfiles directory

创建dotfiles目录

mkdir -p ~/dotfiles cd ~/dotfiles
undefined
mkdir -p ~/dotfiles cd ~/dotfiles
undefined

Structure

目录结构

~/dotfiles/
├── zsh/
│   └── .zshrc
├── git/
│   └── .gitconfig
├── nvim/
│   └── .config/
│       └── nvim/
│           └── init.lua
└── tmux/
    └── .tmux.conf
~/dotfiles/
├── zsh/
│   └── .zshrc
├── git/
│   └── .gitconfig
├── nvim/
│   └── .config/
│       └── nvim/
│           └── init.lua
└── tmux/
    └── .tmux.conf

Usage

使用方法

bash
cd ~/dotfiles
bash
cd ~/dotfiles

Symlink one package

为单个配置包创建软链接

stow zsh
stow zsh

Symlink all

为所有配置包创建软链接

stow */
stow */

Unlink

移除软链接

stow -D zsh
stow -D zsh

Restow (update)

重新配置(更新)

stow -R zsh
stow -R zsh

Target different directory

指定目标目录创建软链接

stow -t ~ zsh
undefined
stow -t ~ zsh
undefined

Chezmoi (Advanced)

Chezmoi(进阶方案)

Setup

配置步骤

bash
undefined
bash
undefined

Install

安装

brew install chezmoi # or sh -c "$(curl -fsLS get.chezmoi.io)"
brew install chezmoi # 或执行 sh -c "$(curl -fsLS get.chezmoi.io)"

Initialize

初始化

chezmoi init
chezmoi init

Or from existing repo

或从已有仓库初始化

Basic Usage

基础使用

bash
undefined
bash
undefined

Add file

添加配置文件

chezmoi add ~/.zshrc chezmoi add ~/.config/nvim/init.lua
chezmoi add ~/.zshrc chezmoi add ~/.config/nvim/init.lua

Edit

编辑配置文件

chezmoi edit ~/.zshrc
chezmoi edit ~/.zshrc

See changes

查看变更

chezmoi diff
chezmoi diff

Apply changes

应用变更

chezmoi apply
chezmoi apply

Update from repo

从仓库更新配置

chezmoi update
undefined
chezmoi update
undefined

Templates

模板功能

bash
undefined
bash
undefined

~/.local/share/chezmoi/dot_gitconfig.tmpl

~/.local/share/chezmoi/dot_gitconfig.tmpl

[user] name = {{ .name }} email = {{ .email }}
[user] name = {{ .name }} email = {{ .email }}

~/.config/chezmoi/chezmoi.toml

~/.config/chezmoi/chezmoi.toml

[data] name = "John Doe" email = "john@example.com"
undefined
[data] name = "John Doe" email = "john@example.com"
undefined

Secrets

敏感信息管理

bash
undefined
bash
undefined

Use 1Password

使用1Password

{{ onepasswordRead "op://Personal/GitHub/token" }}
{{ onepasswordRead "op://Personal/GitHub/token" }}

Use pass

使用pass密码管理器

{{ pass "github/token" }}
{{ pass "github/token" }}

Prompt for value

交互式输入值

{{ promptString "Enter API key" }}
undefined
{{ promptString "Enter API key" }}
undefined

Git Bare Repo

Git裸仓库方案

Setup

配置步骤

bash
undefined
bash
undefined

Initialize

初始化裸仓库

git init --bare $HOME/.dotfiles
git init --bare $HOME/.dotfiles

Alias

设置别名

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

Ignore untracked

忽略未跟踪文件

dotfiles config --local status.showUntrackedFiles no
undefined
dotfiles config --local status.showUntrackedFiles no
undefined

Usage

使用方法

bash
undefined
bash
undefined

Add files

添加配置文件

dotfiles add ~/.zshrc dotfiles commit -m "Add zshrc" dotfiles push
dotfiles add ~/.zshrc dotfiles commit -m "Add zshrc" dotfiles push

Clone to new machine

在新机器上克隆仓库

git clone --bare git@github.com:user/dotfiles.git $HOME/.dotfiles alias dotfiles='git --git-dir=$HOME/.dotfiles --work-tree=$HOME' dotfiles checkout
undefined
git clone --bare git@github.com:user/dotfiles.git $HOME/.dotfiles alias dotfiles='git --git-dir=$HOME/.dotfiles --work-tree=$HOME' dotfiles checkout
undefined

Shell Config

Shell配置

bash
undefined
bash
undefined

Add to ~/.zshrc or ~/.bashrc

添加到~/.zshrc或~/.bashrc

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

Common Dotfiles

常见Dotfiles配置

Essential Files

核心配置文件

~/.zshrc or ~/.bashrc    # Shell config
~/.gitconfig             # Git config
~/.ssh/config            # SSH config
~/.tmux.conf             # Tmux config
~/.config/nvim/          # Neovim config
~/.config/starship.toml  # Starship prompt
~/.zshrc or ~/.bashrc    # Shell配置
~/.gitconfig             # Git配置
~/.ssh/config            # SSH配置
~/.tmux.conf             # Tmux配置
~/.config/nvim/          # Neovim配置
~/.config/starship.toml  # Starship提示符配置

.gitconfig

.gitconfig 示例

ini
[user]
    name = Your Name
    email = your@email.com
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph
[core]
    editor = nvim
[init]
    defaultBranch = main
[pull]
    rebase = true
ini
[user]
    name = Your Name
    email = your@email.com
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    lg = log --oneline --graph
[core]
    editor = nvim
[init]
    defaultBranch = main
[pull]
    rebase = true

.zshrc Essentials

.zshrc 核心配置

bash
undefined
bash
undefined

History

历史记录设置

HISTFILE=~/.zsh_history HISTSIZE=10000 SAVEHIST=10000 setopt SHARE_HISTORY
HISTFILE=~/.zsh_history HISTSIZE=10000 SAVEHIST=10000 setopt SHARE_HISTORY

Aliases

命令别名

alias ll='ls -la' alias g='git' alias dc='docker compose'
alias ll='ls -la' alias g='git' alias dc='docker compose'

Path

环境变量PATH设置

export PATH="$HOME/.local/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"

Editor

默认编辑器设置

export EDITOR='nvim'
undefined
export EDITOR='nvim'
undefined

Bootstrap Script

初始化脚本

bash
#!/bin/bash
bash
#!/bin/bash

bootstrap.sh

bootstrap.sh

set -e
set -e

Install dependencies

安装依赖

if [[ "$OSTYPE" == "darwin"* ]]; then brew install stow git neovim fi
if [[ "$OSTYPE" == "darwin"* ]]; then brew install stow git neovim fi

Clone dotfiles

克隆dotfiles仓库

Stow all

为所有配置包创建软链接

cd ~/dotfiles for dir in */; do stow "$dir" done
echo "Dotfiles installed!"
undefined
cd ~/dotfiles for dir in */; do stow "$dir" done
echo "Dotfiles installed!"
undefined

Best Practices

最佳实践

  1. Version control: Always use git
  2. README: Document setup steps
  3. Modular: Separate by application
  4. Secrets: Never commit secrets; use templates or secret managers
  5. Bootstrap: Create setup script for new machines
  1. 版本控制:始终使用Git进行版本管理
  2. README文档:记录配置步骤与说明
  3. 模块化:按应用程序拆分配置文件
  4. 敏感信息:绝不要提交敏感信息;使用模板或密钥管理器
  5. 初始化脚本:为新机器创建一键配置脚本