TIL #40 - How to Set Up Folding in Neovim | ProductsWay

TIL #40 - How to Set Up Folding in Neovim

Apr 11, 2023 · Dung Huynh

What

Configure fast, Treesitter-aware code folding in Neovim with nvim-ufo and a fold preview popup.

Why

Neovim supports several built-in fold methods, including manual, indent, expression, marker, and syntax folding. nvim-ufo adds convenient Treesitter and indent providers, while fold-preview.nvim shows hidden lines in a popup before you expand a fold.

How

1. Set options:

vim.o.foldcolumn = "1"
vim.o.foldlevel = 99
vim.o.foldenable = true
vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]

2. Install plugins (with lazy.nvim):

{
  "kevinhwang91/nvim-ufo",
  dependencies = { "kevinhwang91/promise-async" },
  opts = {
    provider_selector = function() return { "treesitter", "indent" } end
  },
  init = function()
    vim.keymap.set("n", "zR", require("ufo").openAllFolds)
    vim.keymap.set("n", "zM", require("ufo").closeAllFolds)
  end
},
{ "anuvyklack/fold-preview.nvim", dependencies = "anuvyklack/keymap-amend.nvim" }

Press h on closed fold to preview, press again to open.