Fold with relentless elegance.
A collection of Quality-of-life features related to folding.
requires nvim-ufo
foldtext
, preserving the syntax highlighting of the
line (requires Treesitter parser for the language). incompatible with nvim-ufo
h
key which will fold a line when used one the first non-blank
character of (or before). And overload the l
key, which will unfold a line
when used on a folded line.[^1] This allows you to ditch zc
, zo
, and
za
, h
and l
are all you need.vim.lsp.foldexpr
from
nvim 0.11. incompatible with nvim-ufo
[!NOTE] This plugin does not provide a
foldmethod
. For this plugin to work, you either have to set one of on your own, or use plugin providing folding information, such as nvim-ufo.
-- lazy.nvim
{
"chrisgrieser/nvim-origami",
event = "VeryLazy",
opts = {}, -- needed even when using default config
},
-- packer
use {
"chrisgrieser/nvim-origami",
config = function() require("origami").setup({}) end, -- setup call needed
}
-- default settings
require("origami").setup {
-- requires with `nvim-ufo`
keepFoldsAcrossSessions = package.loaded["ufo"] ~= nil,
pauseFoldsOnSearch = true,
-- incompatible with `nvim-ufo`
foldtextWithLineCount = {
enabled = package.loaded["ufo"] == nil,
template = " %s lines", -- `%s` gets the number of folded lines
hlgroupForCount = "Comment",
},
foldKeymaps = {
setup = true, -- modifies `h` and `l`
hOnlyOpensOnFirstColumn = false,
},
-- redundant with `nvim-ufo`
autoFold = {
enabled = false,
kinds = { "comment", "imports" }, ---@type lsp.FoldingRangeKind[]
},
}
If you use other keys than h
and l
for vertical movement, set
setupFoldKeymaps = false
and map the keys yourself:
vim.keymap.set("n", "<Left>", function() require("origami").h() end)
vim.keymap.set("n", "<Right>", function() require("origami").l() end)
Many formatting plugins open all your folds and unfortunately, there is nothing this plugin can do about it. The only two tools I am aware of that are able to preserve folds are the efm-language-server and conform.nvim.
-- Folds provided by the LSP
require("origami").inspectLspFolds("special") -- comment & import only
require("origami").inspectLspFolds("all")
In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.
I also occasionally blog about vim: Nano Tips for Vim
[^1]: Technically, unfolding with l
is already a built-in vim feature when
vim.opt.foldopen
includes hor
. However, this plugin still sets up a l
key replicating that behavior, since the built-in version still moves you to
one character to the side, which can be considered a bit counterintuitive.