Skip to content

Mac 重装后的一些开发配置

因为一些原因,昨天重装了电脑系统。已经抹掉所有数据之后,我才发现在备份的时候,我忘记了备份用户配置文件......只好重新配一遍了,顺便把过程大致记录一下。这里只记录其中几个比较主要或是比较麻烦的软件安装与配置。

Homebrew

安装 Homebrew

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

安装完成后,按照终端提示运行以下命令将其加入 PATH:

bash
echo >> ~/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv zsh)"

验证安装:

bash
brew --version

每次使用 homebrew 它都会更新半天,很烦,所以我禁用了自动更新。在 ~/.zshrc 添加如下内容:

bash
export HOMEBREW_NO_AUTO_UPDATE=1

zsh

因为备份的时候漏掉了,所以我损失掉了我所有的 zsh 配置。本来我想偷懒改用开箱即用的 fish 的,不过 fish 不是标准 shell,只好重新配置 zsh 了。

不过这次我学聪明了一些,可以把 zsh 的配置拆成多个文件放到一个 git 仓库里,用 .gitignore 去掉包含密钥信息的文件,然后只需要在 ~/.zshrc 中 source 这些文件。这样就可以在保证安全的情况下用 git 在设备之间同步 zsh 配置。

比如配置文件夹目录树如下

$HOME/.config/zsh
├── alias.zsh # 命令别名
├── auth.zsh # 鉴权密钥
├── function.zsh # 功能函数
├── oh-my-zsh.zsh # oh-my-zsh 配置
├── path.zsh # 环境变量 PATH
└── setting.zsh # 其他设置

则在 ~/.zshrc 中编写如下内容

bash
ZSH_CONFIG_DIR="$HOME/.config/zsh"

source $ZSH_CONFIG_DIR/oh-my-zsh.zsh
source $ZSH_CONFIG_DIR/path.zsh
source $ZSH_CONFIG_DIR/function.zsh
source $ZSH_CONFIG_DIR/alias.zsh
source $ZSH_CONFIG_DIR/setting.zsh
source $ZSH_CONFIG_DIR/auth.zsh

好的,其他配置可以按自己的需求写(我的 zsh 配置),这里比较麻烦一点的是一些插件的安装和配置。

先安装 Oh My Zsh:

bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

可以把他的配置照上面说的一样单独拷到一个文件按需修改,依旧禁用自动更新:

zstyle ':omz:update' mode disabled  # disable automatic updates

然后安装两个核心插件:

zsh-autosuggestions(命令补全建议):

bash
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

zsh-syntax-highlighting(命令语法高亮):

bash
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

在 oh-my-zsh 配置中启用这些插件

plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

Miniforge

然后是 Python 包管理 Miniforge,此前我用的是 Miniconda,二者配置方法差不多。

下载安装

bash
# 从 https://github.com/conda-forge/miniforge/releases 下载对应版本
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh

bash Miniforge3-MacOSX-arm64.sh

按提示初始化 shell,然后重启终端或执行:

bash
source ~/.zshrc

验证安装:

bash
conda --version

然后换清华镜像:

bash
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

# 设置搜索优先级
conda config --set show_channel_urls yes

如果需要恢复官方源:

bash
conda config --remove-key channels
conda config --add channels conda-forge

顺便贴一个 pip 换源在这儿,省得每次去搜

bash
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

清除配置即可恢复默认:

bash
pip config unset global.index-url

git & ssh

git 的话就一些基础配置,也是懒得每次去搜,贴一个在这里:

bash
# 设置用户名和邮箱
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"

# 设置代理
git config --global http.proxy http://<proxy_host>:<proxy_port>
git config --global https.proxy https://<proxy_host>:<proxy_port>

顺便配一下远程仓库的免密登录(SSH)

bash
ssh-keygen -t rsa -b 4096 -C "<身份注释,一般是邮箱>"
ssh-copy-id [-p <>] <用户>@<ip地址或域>

其他

这里只记录了目前我觉得装起来比较麻烦的几个东西,其他的话,以后我可能会写一个工具清单,用来列举我觉得比较好用的软件和他们的安装注意事项。