跳转至

Yazi 终端文件管理器

为什么要学 Yazi

Yazi 是一个用 Rust 编写的终端文件管理器,以极致的速度和现代化的特性著称。它支持文件预览(图片、视频、PDF、代码高亮)、异步 I/O、批量重命名、标签页、插件系统和 Vim 式键位。对于经常在终端中操作文件的开发者和系统管理员来说,Yazi 提供了比 ls/cd 更高效、比 ranger 更快的文件管理体验。


核心概念

概念白话解释用途
Panel面板左(父目录)/中(当前目录)/右(预览)
Tab标签页多个文件管理标签
Plugin插件Lua 扩展功能
Opener打开器文件类型关联打开方式
Previewer预览器文件预览渲染器
Task任务后台文件操作(复制/移动等)

安装配置

安装

# macOS
brew install yazi ffmpegthumbnailer unar jq poppler fd ripgrep fzf zoxide imagemagick

# Cargo
cargo install --locked yazi-fm yazi-cli

# Arch Linux
pacman -S yazi ffmpegthumbnailer unarchiver jq poppler fd ripgrep fzf zoxide imagemagick

# 预览依赖(可选但推荐)
# ffmpegthumbnailer - 视频缩略图
# unar - 压缩包预览
# jq - JSON 预览
# poppler - PDF 预览
# fd - 文件搜索
# ripgrep - 内容搜索
# fzf - 模糊搜索
# imagemagick - 图片处理

配置文件位置

~/.config/yazi/
├── yazi.toml       # 主配置
├── keymap.toml     # 快捷键
├── theme.toml      # 主题
└── plugins/        # 插件目录

基础配置

# ~/.config/yazi/yazi.toml
[manager]
ratio = [1, 3, 4]          # 三栏比例
sort_by = "natural"
sort_dir_first = true
show_hidden = false
show_symlink = true

[preview]
tab_size = 2
max_width = 600
max_height = 900
image_filter = "triangle"
image_quality = 75

[opener]
edit = [
  { run = '${EDITOR:-nvim} "$@"', block = true, for = "unix" },
]
open = [
  { run = 'xdg-open "$@"', for = "linux" },
  { run = 'open "$@"', for = "macos" },
]

快速上手

基本操作

导航:
  h/l         进入父目录/进入目录(或打开文件)
  j/k         上下移动
  J/K         向下/向上滚动预览
  gg/G        跳到顶部/底部
  ~           回到主目录

文件操作:
  Space       选中/取消选中
  V           进入可视选择模式
  y           复制(yank)
  x           剪切
  p           粘贴
  d           移到回收站
  D           永久删除
  a           新建文件/目录
  r           重命名
  .           显示/隐藏隐藏文件

搜索:
  /           搜索文件名
  s           用 fd 搜索
  S           用 rg 搜索内容

标签页:
  t           新标签页
  1-9         切换标签页
  Ctrl+c      关闭标签页

其他:
  w           显示任务管理器
  z           用 zoxide 跳转
  q           退出

Shell 集成(cd 到最后目录)

# Bash/Zsh: 添加到 .bashrc/.zshrc
function y() {
  local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
  yazi "$@" --cwd-file="$tmp"
  if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
    builtin cd -- "$cwd"
  fi
  rm -f -- "$tmp"
}

# Fish
function y
  set tmp (mktemp -t "yazi-cwd.XXXXXX")
  yazi $argv --cwd-file="$tmp"
  if set cwd (command cat -- $tmp); and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
    builtin cd -- "$cwd"
  end
  rm -f -- $tmp
end

进阶用法

自定义快捷键

# ~/.config/yazi/keymap.toml
[[manager.prepend_keymap]]
on = ["<C-n>"]
run = "create"
desc = "新建"

[[manager.prepend_keymap]]
on = ["T"]
run = "shell 'eza -la --icons' --block"
desc = "列出详细信息"

[[manager.prepend_keymap]]
on = ["g", "d"]
run = "cd ~/Downloads"
desc = "跳转到下载目录"

[[manager.prepend_keymap]]
on = ["g", "p"]
run = "cd ~/projects"
desc = "跳转到项目目录"

主题定制

# ~/.config/yazi/theme.toml
[filetype]
rules = [
  { name = "*/", style = { fg = "blue", bold = true } },
  { mime = "text/*", style = { fg = "green" } },
  { mime = "image/*", style = { fg = "yellow" } },
  { mime = "video/*", style = { fg = "magenta" } },
  { mime = "audio/*", style = { fg = "cyan" } },
  { name = "*.py", style = { fg = "yellow" } },
  { name = "*.rs", style = { fg = "red" } },
  { name = "*.go", style = { fg = "cyan" } },
  { name = "*.md", style = { fg = "white", italic = true } },
]

插件使用

# 安装插件
ya pack -a yazi-rs/plugins:full-border
ya pack -a yazi-rs/plugins:chmod

# 或手动克隆到插件目录
git clone https://github.com/yazi-rs/plugins.git ~/.config/yazi/plugins
# yazi.toml 中启用插件
[[plugin.prepend_previewers]]
name = "*.md"
run = "glow"  # 使用 glow 预览 Markdown

# keymap.toml 中绑定插件快捷键
[[manager.prepend_keymap]]
on = ["c", "m"]
run = "plugin chmod"
desc = "修改权限"

批量重命名

1. 选中多个文件(Space 或 V 模式)
2. 按 r 进入批量重命名
3. 在编辑器中修改文件名
4. 保存退出自动执行重命名

图片预览配置

# yazi.toml
[preview]
image_filter = "triangle"    # 图片缩放算法
image_quality = 75           # 图片质量

# 终端需要支持图形协议:
# Kitty Graphics Protocol(Kitty, WezTerm)
# Sixel(部分终端)
# iTerm2 Inline Images Protocol
# ueberzugpp(X11 终端的备选方案)

常见问题

Q: 图片预览不显示?

需要终端支持图形协议。推荐使用 Kitty、WezTerm 或 iTerm2 终端。在 SSH 环境下可以使用 ueberzugpp。

Q: 与 ranger 相比?

  • Yazi:Rust 编写,速度快 5-10 倍,异步 I/O 不卡顿
  • ranger:Python 编写,插件生态更成熟,但大目录会卡

Q: 如何在 Tmux 中使用图片预览?

需要 Tmux 3.3+ 并启用 allow-passthrough

# tmux.conf
set -g allow-passthrough on
set -ga update-environment TERM
set -ga update-environment TERM_PROGRAM


参考资源

  • GitHub:https://github.com/sxyazi/yazi
  • 文档:https://yazi-rs.github.io/
  • 配置参考:https://yazi-rs.github.io/docs/configuration/yazi
  • 插件:https://yazi-rs.github.io/docs/plugins/overview
  • 主题:https://github.com/yazi-rs/flavors