欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > ubuntu24.04终端优化(Zsh + Oh My Zsh)

ubuntu24.04终端优化(Zsh + Oh My Zsh)

2025/4/16 5:07:04 来源:https://blog.csdn.net/u014796292/article/details/147208276  浏览:    关键词:ubuntu24.04终端优化(Zsh + Oh My Zsh)

文章目录

  • ubuntu24.04终端优化
    • 一、安装Zsh
    • 二、安装Oh My Zsh(管理框架)
    • 三、主题推荐
      • 1. 安装字体
      • 2. 安装主题
      • 3. 修改配置文件
      • 4. 重新加载配置后会自动运行配置向导
      • 5. 其他主题推荐
    • 四、推荐插件安装
      • 1. 语法高亮
      • 2. 自动补全
      • 3. 其他实用插件
      • 4.插件优化技巧
    • 五、其他优化技巧
      • 1. 历史命令优化
      • 2. 别名设置
      • 3. 启用颜色支持
    • 六、最终生效
    • 七、恢复 bash
    • 八、注意事项

ubuntu24.04终端优化

一、安装Zsh

在bash中执行以下命令:

# 安装
sudo apt update && sudo apt install zsh -y
# 设置为默认终端
chsh -s $(which zsh)

重启系统或者重新登陆后生效

二、安装Oh My Zsh(管理框架)

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

三、主题推荐

1. 安装字体

安装 Powerlevel10k,需要预先下载主题所需字体

# 下载安装所需字体
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf

双击下载的字体并安装,在终端–配置文件首选项–配置文件(默认配置)–自定义字体中,选择“MesloLGS NF Regular”或者“MesloLGS NF”

2. 安装主题

推荐 Powerlevel10k 主题

git clone --depth=1 <https://github.com/romkatv/powerlevel10k.git> ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k

3. 修改配置文件

~/.zshrc 文件中将主题设置为 Powerlevel10k

ZSH_THEME="powerlevel10k/powerlevel10k"

4. 重新加载配置后会自动运行配置向导

source ~/.zshrc

应该会自动弹出powerlevel10k的主题设置,如果未弹出,输入p10k configure即可手动进入设置页面,后续修改也可使用p10k configure进入修改

5. 其他主题推荐

  • Spaceship:前端/全栈开发
  • Starship:多 Shell 环境
  • Pure:追求纯净体验
  • Agnoster:服务器管理
  • Powerline:与 Vim 的 powerline 状态栏风格统一
  • Dracula:长时间编码

四、推荐插件安装

1. 语法高亮

sudo apt install zsh-syntax-highlighting -y
echo "source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc

2. 自动补全

sudo apt install zsh-autosuggestions -y
echo "source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh" >> ~/.zshrc

3. 其他实用插件

  • zsh-vi-mode:将 Zsh 变成完整的 Vi/Vim 编辑模式

    git clone https://github.com/jeffreytse/zsh-vi-mode ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-vi-mode
    
  • forgit:交互式 git 命令增强

    git clone https://github.com/wfxr/forgit.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/forgit
    
  • zsh-autocomplete:输入时实时显示补全建议

    git clone https://github.com/marlonrichert/zsh-autocomplete.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autocomplete
    

下载完成后,编辑 ~/.zshrc 修改插件配置,在 plugins数组 后添加对应的插件名称:

plugins=(git sudo docker docker-compose kubectl ubuntu forgit zsh-autocomplete)

4.插件优化技巧

性能优化配置( .zshrc 中添加)

# 按需加载插件(减少启动时间)
zstyle ':omz:plugins' lazy-load yes# 指定延迟加载的插件
zstyle ':omz:plugins:lazy' aliases git docker

五、其他优化技巧

1. 历史命令优化

~/.zshrc 中添加:

HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory
setopt share_history
setopt hist_ignore_all_dups

2. 别名设置

~/.zshrc 中添加常用别名:

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias update='sudo apt update && sudo apt upgrade -y'
alias cls='clear'

3. 启用颜色支持

echo 'export CLICOLOR=1' >> ~/.zshrc
echo 'export LS_COLORS="di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43"' >> ~/.zshrc

六、最终生效

source ~/.zshrc

七、恢复 bash

# 如果需要切换回bash为默认终端
chsh -s $(which bash)
# 如果临时切换回bash
bash

八、注意事项

  • 插件太多可能会影响启动速度,建议按需安装
  • 主题建议先选择自己喜欢的风格再安装
  • 对于 Powerlevel10k 主题,使用p10k configure随时进行风格更换

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词