Skip to content

我的 MacOS 开发环境配置

Updated:

Table of contents

展开目录

系统设置

程序坞自动隐藏加速

# 设置启动坞动画时间设置为 0.5 秒
defaults write com.apple.dock autohide-time-modifier -float 0.5 && killall Dock

# 设置启动坞响应时间最短
defaults write com.apple.dock autohide-delay -int 0 && killall Dock

# 恢复启动坞默认动画时间
defaults delete com.apple.dock autohide-time-modifier && killall Dock

# 恢复默认启动坞响应时间
defaults delete com.apple.Dock autohide-delay && killall Dock

启动台自定义行和列

# 设置列数
defaults write com.apple.dock springboard-columns -int 7

# 设置行数
defaults write com.apple.dock springboard-rows -int 6

# 重启 Dock 生效
killall Dock

# 恢复默认的列数和行数
defaults write com.apple.dock springboard-rows Default
defaults write com.apple.dock springboard-columns Default

# 重启 Dock 生效
killall Dock

常用软件安装

fastfetch

命令行工具

统一采用 Homebrew 对系统软件包进行管理,安装命令行工具时,优先考虑 Homebrew 提供的软件包。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

差生文具多:

PreferDescriptionReplace
ezaA modern, maintained replacement for ls.ls
zoxideA smarter cd command, supports all major shells.cd
htopAn interactive process viewer.top
ripgrepripgrep recursively searches directories for a regex pattern while respecting your gitignore.grep
ncduNcdu (NCurses Disk Usage) is a disk usage analyzer with an ncurses interface.du
batA cat(1) clone with wings.cat
fdA simple, fast and user-friendly alternative to ‘find’.find
procsA modern replacement for ps written in Rust.ps
fzfA general-purpose command-line fuzzy finder written in Go.find
fzf-tabReplace zsh’s default completion selection menu with fzf.find
tokeiA program that displays statistics about your code.cloc
tldrCollaborative cheatsheets for console commands.man
thefuckMagnificent app which corrects your previous console command.-
lazygitA simple terminal UI for git commands.git
lazydockerA simple terminal UI for both docker and docker-compose.docker
fastfetchLike neofetch, but much faster because written mostly in C.neofetch
deltaA syntax-highlighting pager for git, diff, and grep output.diff

终端利器 Fzf

在日常的 shell 环境中,可以通过 Fzf + Ripgrep + Bat + Vim 来提升效率,方便地管理文件及其内容。

可以将以上内容添加至 ~/.zshrc 文件中,从而构建一个更加高效的命令行 Workflow。

Zsh 配置

常用的 Oh My Zsh 对于我来说略显臃肿,因此我选择使用 zinit 来管理插件。

至此,~/.zshrc 可以分为几个模块,分别定义不同的功能配置:

zinit 相关

在安装 zinit 的过程中,它会自动的接管 ~/.zshrc,并向其中添加其相关配置如下:

### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
    command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
    command git clone https://github.com/zdharma-continuum/zinit "$HOME/.local/share/zinit/zinit.git" && \
        print -P "%F{33} %F{34}Installation successful.%f%b" || \
        print -P "%F{160} The clone has failed.%f%b"
fi

source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
    zdharma-continuum/zinit-annex-as-monitor \
    zdharma-continuum/zinit-annex-bin-gem-node \
    zdharma-continuum/zinit-annex-patch-dl \
    zdharma-continuum/zinit-annex-rust

### End of Zinit's installer chunk

加载环境变量

# Load Environment Variables
export PATH="/Users/lkw123/Library/Python/3.9/bin:$HOME/.cargo/bin:$PATH"
export BAT_THEME="Monokai Extended Origin"
export STARSHIP_CONFIG="/Users/lkw123/.config/starship/starship.toml"
export GPG_TTY=$(tty)

三个基础插件

# Add in zsh plugins
zinit light zdharma-continuum/fast-syntax-highlighting
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions

# Load completions
autoload -Uz compinit && compinit

# Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no

引入 fzf-tab

值得注意的一点是,根据 fzf-tab 的 README 中的 compatibility-with-other-plugins 所言,需要将 fzf-tab 的引入放在配置文件的最后部分,以避免和 zsh-completions 插件产生冲突。

# fzf-tab init and styling
zinit light Aloxaf/fzf-tab
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza --icons -1 --color=always $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'eza --icons -1 --color=always $realpath'
zstyle ':fzf-tab:complete:z:*' fzf-preview 'eza --icons -1 --color=always $realpath'

通过以上的配置,可以实现在 cd 目录时,配合 fzf 的功能快速预览目标目录的文件结构:

定义别名

# Aliases
alias ls="eza"
alias ll="eza --time-style=default --icons --git -l"
alias la="eza --time-style=long-iso --icons --group --git --binary -la"
alias tree="eza --tree --icons"
alias cls="clear"
alias cat="bat"
alias v="nvim"

历史记录

# History
HISTSIZE=5000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
setopt appendhistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups

Shell 集成

# Shell integrations
eval "$(starship init zsh)"
eval "$(thefuck --alias)"
eval "$(fzf --zsh)"
eval "$(zoxide init zsh)"
eval "$(fnm env --use-on-cd)"

最后,简单测试一下 Zsh 的启动时间:

  1. 采用直观的比较 Naive 的方式直接观测 zsh 的启动速度:

     for i in $(seq 10); do
      /usr/bin/time zsh -lic exit
    done
    
        0.16 real         0.10 user         0.04 sys
        0.13 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
        0.14 real         0.10 user         0.03 sys
        0.14 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
        0.13 real         0.09 user         0.03 sys
    
  2. 借助 zsh-bench 更完善的对 zsh 的启动时间进行基准测试:

     ./zsh-bench
    ==> benchmarking login shell of user lkw123 ...
    creates_tty=0
    has_compsys=1
    has_syntax_highlighting=1
    has_autosuggestions=1
    has_git_prompt=1
    first_prompt_lag_ms=199.405
    first_command_lag_ms=202.526
    command_lag_ms=59.470
    input_lag_ms=4.480
    exit_time_ms=136.965
    

确实挺不错的 :)

开发工具

GUI 工具

统一采用 Homebrew Cask 进行管理。

$ brew list
==> Formulae
...
==> Casks
1password                       iina                    plex
alacritty                       jellyfin                sabnzbd
applite                         loop                    transmit
daisydisk                       lulu                    tuxera-ntfs
downie                          netnewswire             typora
font-jetbrains-mono-nerd-font   notion                  zed
font-lxgw-wenkai                orbstack
handbrake                       pictureview

开发环境

Tmux 配置

感谢 B 站 UP 主帕特里柯基在视频 和我一起配置 tmux 中分享的配置过程。我的 Tmux 配置基本是照搬过来:

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'tmux-plugins/tmux-yank'

# catppuccin theme
set -g @plugin "catppuccin/tmux"
set -g @catppuccin_flavour "mocha"

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

# non-plugin options
set -g default-terminal "tmux-256color"
set -g base-index 1
set -g pane-base-index 1
set -g renumber-windows on
set -g mouse on

# visual mode
set-window-option -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

# keymaps
unbind C-b
set -g prefix C-Space

MacOS 软件一键更新

准备工作

更新软件

升级所有已安装的软件:

brew update && brew upgrade && brew cu --all --yes --cleanup \
&& mas upgrade && brew cleanup
  1. brew update:更新 Homebrew 自身及其相关软件源的信息,确保 Index 信息准确;

  2. brew upgrade:将系统中已安装的所有 Homebrew 软件包升级到最新版本;

  3. brew cu --all --yes --cleanup:自动升级所有可更新的 Homebrew cask 软件包到最新版本,并在完成后清理删除旧版本;

  1. mas upgrade: 一键更新从 Mac App Store 安装的应用程序;

  2. brew cleanup: 用于清理 Homebrew 安装的软件包时产生的临时文件和缓存,以释放磁盘空间。

执行结果样例如下:

可以作为定时任务,周期性执行,以保持 Mac 上软件为最新版本。


我的部分 dotfiles 托管于 GitHub 仓库 synthpop123/dotfiles,仅用于备份用途。