1. WSL 安装
对于 Windows 11 用户
- 以管理员身份打开 PowerShell 或 Windows 终端,输入:
wsl --install
- 重启电脑。
- 重启后,再次运行:
wsl --install
- 设置 Ubuntu 用户名和密码。
- 更新系统包:
sudo apt update && sudo apt upgrade -y
2. ZSH 安装与配置
- 安装 ZSH:
sudo apt install zsh -y
- 将 ZSH 设为默认 shell:
chsh -s $(which zsh)
- 注销并重新登录以使更改生效。
3. Oh My Zsh 安装与自定义
- 安装 Oh My Zsh:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
- 安装 Spaceship 主题:
git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"
- 编辑
~/.zshrc
文件,设置主题:
ZSH_THEME="spaceship"
4. ZSH 插件配置
- 安装常用插件:
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
- 编辑
~/.zshrc
文件,添加以下配置:
# Oh My Zsh 配置
export ZSH="$HOME/.oh-my-zsh"
# 主题设置
ZSH_THEME="spaceship"
# Spaceship 主题自定义
SPACESHIP_PROMPT_ORDER=(
time # 时间戳
user # 用户名
dir # 当前目录
host # 主机状态
git # Git 状态
node # Node.js 环境
# ruby # Ruby 环境
# xcode # Xcode 环境
# swift # Swift 环境
# golang # Go 环境
# php # PHP 环境
# rust # Rust 环境
# haskell # Haskell 环境
# julia # Julia 环境
docker # Docker 状态
# aws # Amazon Web Services (AWS) 环境
venv # virtualenv 状态
# conda # conda 环境
# pyenv # Pyenv 环境
# kubectl # Kubectl 上下文
terraform # Terraform 工作区
exec_time # 命令执行时间
line_sep # 行分隔符
battery # 电池电量
jobs # 后台任务指示器
exit_code # 上一个命令的退出代码
char # 提示符字符
)
SPACESHIP_TIME_SHOW=true
SPACESHIP_USER_SHOW="always"
SPACESHIP_USER_COLOR="#f783ac"
SPACESHIP_TIME_COLOR="#d3adf7"
# 插件配置
plugins=(
git
zsh-syntax-highlighting
zsh-autosuggestions
docker
docker-compose
npm
nvm
python
pip
virtualenv
golang
rust
)
source $ZSH/oh-my-zsh.sh
# 实用别名
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias p="pnpm"
alias pi="pnpm install"
alias pr="pnpm run"
alias pd="pnpm run dev"
alias pb="pnpm run build"
alias gpl="git pull"
alias gps="git push"
alias ga="git add"
alias gc="git commit"
alias gcm="git commit -am"
alias gco="git checkout"
alias gcb="git checkout -b"
# 代理设置函数
function proxy_on() {
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy="http://127.0.0.1:7890"
export https_proxy=$http_proxy
export all_proxy=socks5://127.0.0.1:7890
echo -e "\033[32m代理已开启\033[0m"
curl -i https://www.google.com
# 添加新功能
echo "代理已开启,当前时间: $(date)"
}
function proxy_off() {
unset http_proxy https_proxy all_proxy
echo -e "\033[31m代理已关闭\033[0m"
# 添加新功能
echo "代理已关闭,当前时间: $(date)"
}
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
5. 开发环境配置
Node.js (使用 NVM)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.zshrc
nvm install --lts
Python
sudo apt install python3 python3-pip python3-venv
Go
wget https://go.dev/dl/go1.20.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.20.5.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
source ~/.zshrc
Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
Docker
sudo apt install docker.io
sudo usermod -aG docker $USER
6. ZSH 性能优化
- 限制历史记录大小:
echo 'HISTSIZE=5000' >> ~/.zshrc
echo 'SAVEHIST=5000' >> ~/.zshrc
- 使用
zprof
分析启动时间:
zsh -i -c -x exit
- 考虑使用
zinit
进行插件管理:
bash -c "$(curl --fail --show-error --silent --location https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"
然后在 ~/.zshrc
中添加:
source "$HOME/.zinit/bin/zinit.zsh"
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-syntax-highlighting
7. 高级技巧与最佳实践
- 使用
Ctrl+R
进行历史命令搜索。 - 利用
Tab
进行智能补全。 - 使用
cd -
快速返回上一个目录。 - 使用
!!
重复执行上一条命令。 - 使用
Ctrl+A
和Ctrl+E
快速移动到行首和行尾。 - 创建自定义函数,例如:
function mkcd() {
mkdir -p "$@" && cd "$_"
}
- 使用
alias
创建常用命令的快捷方式:
alias update='sudo apt update && sudo apt upgrade -y'
- 定期更新 Oh My Zsh 和插件:
omz update
- 使用
zsh-completions
增强补全功能:
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
然后在 ~/.zshrc
的 plugins
数组中添加 zsh-completions
。
- 配置
fzf
进行模糊搜索:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
结语
通过以上配置,您的 WSL 和 ZSH 环境将变得极其强大和高效。随着使用,您会发现更多适合自己工作流程的配置和插件。记得经常回顾和优化您的配置,以确保它始终满足您的需求。