Skip to content
0

Neovim 使用折叠

2024 年 7 月刚入坑 Vim 时,在 Vim 中曾经短暂尝试过「折叠」功能。因为比较少场景使用 Markdown 处理大文件、长文件,很多关于「折叠」的功能不用就忘记了。

最近看到一个 YouTube 博主在介绍 Neovim 中的折叠功能,发现折叠已经内置于 Vim 和 Neovim,新菜上手无需额外插件或配置。但还是需要快速地学习一下相关命令,才能正常使用「折叠」。

  • 官方文档:https://neovim.io/doc/user/fold.html#fold-commands
  • Vim 和 Neovim 均已经默认 foldmethodmanual,运行 :set foldmethod? 可以得到 foldmethod=manual
  • 在 Markdown 文档中,可以先简单尝试默认的 manual 方式
  • 使用 Vim 的话,在 ~/.vimrc 中,可以手动 set foldmethod=marker 切换折叠方式
  • 后续在代码文件中,可以再尝试 indent 方式,需要在配置文件 options.lua 内显式更改配置
  • 关于折叠的命令大都由 z 引导,Neovim 配置了 which-key 插件后,按下引导键 z 之后,会显示全部可用命令的提示菜单

The foldmethod

The six methods of creating folds are:

  • diff The differences between two files define folds.
  • expr Regular expressions define folds.
  • indent Folds and fold levels correspond to the indentation of text and the value of the option shiftwidth.
  • manual Folds and fold levels result from user Vim commands (for example, folding a paragraph).
  • marker Predefined (but also user-definable) markers in the file specify fold boundaries.
  • syntax Folds correspond to the semantics of a file’s language (e.g., a C program’s function blocks could fold).

The Fold Commands

  • zA Toggle the state of folds, recursively.
  • zC Close folds, recursively.
  • zD Delete folds, recursively.
  • zE Delete all folds.
  • zf Create a fold from the current line to the one where the following motion command takes the cursor.
  • [count] zF Create a fold covering count lines, starting with the current line.
  • zM Set option foldlevel to zero.
  • zN, zn Set (zN) or reset (zn) the foldenable option.
  • zO Open folds, recursively.
  • za Toggle the state of one fold.
  • zc Close one fold.
  • zd Delete one fold.
  • zi Toggle the value of the foldenable option.
  • zj, zk Move the cursor to the start (zj) of the next fold or to the end (zk) of the previous fold. Note the mnemonic of the j and k motion commands and how they are analogous to motions within the context of folds.
  • zm, zr Decrement (zm) or increment (zr) the value of the foldlevel option by one.
  • zo Open one fold.

创建折叠

并非必须在一个定义域符号内(比如括号),完全无关的两行也能折叠。

  • zf{motion} / {Visual}zf
  • {count}zF
  • :{range}fo[ld]

比如,zf7j 就可将“当前行 + 往下 7 行”这个区域一下折叠起来。折叠区域在甫一创建,即呈「关闭」状态。

打开折叠

  • zo: 打开光标处的折叠
  • zO: 递归地打开光标处的所有折叠
  • zv: 一直打开折叠直到光标处能显示
  • :{range}foldo[pen][!]:
    • 打开 {range} 范围内一个等级的折叠
    • 如果使用 !,则打开范围内的所有折叠
  • zn: 重置 foldenable,打开所有折叠
  • zN: 设置 foldenable,所有折叠恢复到原来的状态
  • zi: 倒置 foldenable

关闭折叠

处于「打开」状态的折叠区域,可以通过 zczC 进行关闭。

  • zc:
    • 关闭光标处的折
    • 有嵌套时,仅关闭光标所在处最内层的折叠
  • zC: 递归地关闭光标处的所有折叠
  • :{range}foldc[lose][!]:
    • 关闭 {range} 范围内一个等级的折叠
    • 如果使用 !,则关闭范围内的所有折叠

开关折叠

切换折叠区域的「打开」和「关闭」状态。

  • 小于 foldminlines 所设置的行(默认值是 1)将始终显示
  • 关闭折叠将设置 foldenable
  • foldenable fen: 当值为 off 时,所有折叠将被打开,默认值为 on

  • za: 切换光标处的折叠状态
  • zA: 递归地切换光标处的所有折叠状态

删除折叠

  • zd
    • 删除光标处的折叠
    • 有嵌套时,删除外层折叠,内层折叠将上浮一个等级
    • 光标在内层折叠处执行删除,则不影响外层
    • 有删除比预期更多的折叠的风险
  • zD: 递归地删除光标处的所有折叠
  • zE: 删除当前窗口中的所有折叠

不熟练

  • zx: 撤销手动打开或关闭的折叠
  • zX: 递归地撤销手动打开或关闭的折叠
  • zm: 从 foldlevel 减去 v:count1
  • zM: 关闭所有折叠,并将 foldlevel 设为 0
  • zr: 将 v:count1 加入 foldlevel
  • zR: 打开所有折叠,设置 foldlevel 为最高等级

在折叠处移动

  • [z: 移动到折叠的开始处
  • ]z: 移动到折叠的结束处
  • zj: 移动到下一个折叠的开始处
  • zk: 移动到上一个折叠的关闭处
最近更新