Ubunutu终端配置

最终成果

image-20250818130945256
image.png

zshell

安装 zshell

sudo apt update && sudo apt install -y zsh git curl
zsh --version
which zsh

若命令输出如 /usr/bin/zsh,说明安装成功并且可执行路径已就绪。

设为默认终端

将 zsh 设为当前用户的默认登录 Shell,推荐用 chsh 并指向 zsh 的绝对路径。

chsh -s $(which zsh)

注销后重新登录,或新开一个终端,确认默认 Shell:

echo $SHELL

正常应看到 /usr/bin/zsh/bin/zsh;若 chsh 报错或无效,检查 /etc/shells 是否包含 zsh 路径,必要时追加并重试。

安装 oh-my-zsh

Oh My Zsh 是 zsh 的配置框架,推荐使用官方安装脚本(可用 curl 或 wget),国内网络环境可用 install.ohmyz.sh 镜像地址
使用 GitHub Raw:

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

或使用镜像:

sh -c "$(curl -fsSL https://install.ohmyz.sh/)"

安装过程中脚本会建议切换默认 Shell 为 zsh,若之前已切换可直接确认继续;安装后会生成新的 ~/.zshrc,并将旧文件备份为 ~/.zshrc.pre-oh-my-zsh

Dracula 主题

# 下载主题包
git clone https://github.com/dracula/zsh.git
# 进入下载的文件夹
cd zsh
# 设置主题
mv dracula.zsh-theme ~/.oh-my-zsh/themes
mv lib ~/.oh-my-zsh/themes/
# 使配置生效
source ~/.zshrc

如果要完全统一,不仅要把 zshell 设置为 dracula 主题,最好也把终端模拟器(如 Windows Terminal,item 2,kitty 等)的主题也设置为 Dracula

安装 zsh 插件

autosuggestions

当我们输入命令过程中,可以预览历史命令,按 → 便可输出这些命令。

  1. 克隆项目到本地 $ZSH_CUSTOM/plugins  路径下
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  1. 编辑 ~/.zshrc 配置文件,添加到plugins列表中
plugins=(zsh-autosuggestions)

autojump

j <目录名> 的方式快速跳转到以前访问过的目录。

  1. 克隆代码
git clone https://github.com/wting/autojump.git
  1. 安装
cd autojump
./install.py or ./uninstall.py
  1. 配置:编辑 ~/.zshrc 配置文件,添加到plugins列表中
plugins=(autojump)

zsh-syntax-highlighting

在 shell 中高亮语法。

  1. 克隆代码
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  1. 配置:编辑 ~/.zshrc 配置文件,添加到plugins列表中
plugins=(zsh-syntax-highlighting)

安装 NerdFront 字体

访问 Nerd Fonts 网站下载一个自己喜欢的字体。解压,然后点击安装每一个字形。
3.png

设置 Ubununtu 终端字体(其他终端软件同理):

  • 点击右上角选项,打开Preference
  • 选择刚刚下载的字体,注意需要是Mono 后缀的(保证等宽,代码才能对齐)
    1.png
    2.png

安装 lsd

lsd 可以为 ls 命令中的不同类型文件或文件夹添加不同颜色, 并且使用对应的图标进行标识, 简洁直观。
image.png

  • 使用 apt 安装:如果找不到包,说明 ubuntu 版本不够,采用 cargo 安装。
sudo apt update && sudo apt install -y lsd
  • Cargo 安装
wget https://github.com/lsd-rs/lsd/releases/download/v1.1.5/lsd-musl_1.1.5_amd64_xz.deb -o /tmp/lsd.deb
# 下载失败可以尝试使用以下命令
wget https://ghproxy.net/https://github.com/lsd-rs/lsd/releases/download/v1.1.5/lsd-musl_1.1.5_amd64_xz.deb -O /tmp/lsd.deb

sudo apt install /tmp/lsd.deb
lsd -V

lsd 命令举例:

  • lsd: 最基础命令, 显示当前目录结构
  • lsd -l: 以竖向列表显示当前目录结构
  • lsd -la: 以竖向列表显示当前目录结构(含隐藏文件)
  • lsd --tree: 以目录树格式显示当前目录结构

如果图标显示不出来,是因为没有安装 NerdFront 字体

可以在 .zshrc 中添加别名:

alias ls='lsd'
alias ll='lsd -al'
alias lt='lsd --tree'

安装 homebrew

很多时候用 apt 找不到的包,都可以用 homebrew 安装。

sudo apt-get update
sudo apt-get install build-essential curl file git

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

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc

brew --version

安装 fzf

它是一个用于任何类型列表的交互式过滤程序;无论是文件、命令历史、进程、主机名、书签还是 Git 提交等。它实现了一种“模糊”匹配算法,因此您可以快速输入省略字符的模式,仍然获得想要的结果。

# 下载源码
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
# 安装
~/.fzf/install

然后重启 shell,就可以开始使用了,如果每次打开终端都要开启 fzf 的 shell 集成,还需要在 ~/.zshrc 中添加下面一行代码。

source <(fzf --zsh)

默认快捷键:

  • ctrl t :搜索当前目录
  • ctrl r :搜索历史
  • [搜索界面中]ctrl j :,下一个选项
  • [搜索界面中]ctrl k :上一个选项
  • [搜索界面中]tab :多选
    Shell 集成: 连续输入 ** 后按下 tab 即可触发搜索。
# 搜索文件或目录
cd **<TAB>
vim **<TAB>
# 搜索进程
kill -9 **<TAB>
# 搜索主机名
ssh **<TAB>
telnet **<TAB>
# 搜索环境变量,别名
unset **<TAB>
export **<TAB>
unalias **<TAB>

预览功能:fzf 提供了一些预览脚本,比如 fzf --preview 'fzf-preview.sh {} 可以实现预览当前目录的文件。
image.png

如果要预览图片,需要安装终端图片预览工具。

添加预览命令的别名:在 ~/.zshrc 中添加如下命令,之后执行 source ~/.zshrc 即可生效。

alias fsfp="fzf --style full --preview 'fzf-preview.sh {}' --bind 'focus:transform-header:file --brief {}'"

安装图片预览工具

你的 fzf-preview.sh 脚本需要调用这些工具来生成图片预览。你至少需要安装其中一个。

  • viu:一个比较通用的工具,兼容多种终端,但在不支持的终端上会用字符块(block characters)来近似显示。
  • chafa:功能强大,支持多种协议,也可以用字符块显示,效果通常比 viu 好。
  • icat:专门为 Kitty 终端设计,效果最好。
  • imgcat:专门为 iTerm2 终端设计。
# 使用homebrew安装
brew install viu chafa
# 找一张图片测试
viu /path/to/your/image.jpg
# 或者
chafa /path/to/your/image.png

image.png

完整.zshrc文件

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH

# Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="dracula"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
git
autojump
zsh-autosuggestions
zsh-syntax-highlighting
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='nvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch $(uname -m)"

# Set personal aliases, overriding those provided by Oh My Zsh libs,
# plugins, and themes. Aliases can be placed here, though Oh My Zsh
# users are encouraged to define aliases within a top-level file in
# the $ZSH_CUSTOM folder, with .zsh extension. Examples:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# alias
alias ls='lsd'
alias ll='lsd -l'
alias lt='lsd --tree'
alias fzfp="fzf --style full --preview 'fzf-preview.sh {}' --bind 'focus:transform-header:file --brief {}'"

# zsh代码高亮插件
source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# fzf的shell集成
source <(fzf --zsh)

export PATH=$PATH:/Users/iven/.spicetify:/Applications/WezTerm.app/Contents/MacOS
posted @ 2025-08-16 15:17  Miaops  阅读(232)  评论(0)    收藏  举报