skip to content
IMO's IMO

Using folding markers to str

/ 2 min read

The Problem

It’s easy to get lost while moving around in a long file. Sometimes the IDE is able to digest it and offer folding and navigation features, but not always… This is usually the case when dealing with long configuration files (e.g. tmux.conf or .bahsrc).

The Solution

One nice way I have found to help is by using folding markers in comments to add that missing structure.

1. Open the file in a vim-based editor

I use neovim, but this should work in any vim-based editor.

2. Set the folding method to “marker”

Terminal window
set foldmethod=marker
set foldenable
set foldlevel=0

3. Place your markers

Implied levelStyle
1{{{1 H1
2| {{{1 H1
3| | {{{1 H1

They are all “level 1” markers so that they stay visible at the highest folding level.

Despite using level 1 markers, the optional “| ” helps to visually differentiate the implied levels.

4. Use the folding commands to navigate the file

The most useful ones are:

commandFunction
zjmoves the cursor to the next fold.
zkmoves the cursor to the previous fold.
zMcloses all open folds.
zRdecreases the foldlevel to zero — all folds will be open

An example file

This is more or less what it would look like for a small file:

autocmds.lua
-- {{{1 Disable autoformat for some filetypes
7 collapsed lines
vim.api.nvim_create_autocmd({ "FileType" }, {
pattern = { "python", "toml" },
callback = function()
vim.b.autoformat = false
end,
})
-- {{{1 Add new filetype patterns
1 collapsed line
-- | {{{1 nix
4 collapsed lines
vim.filetype.add({
filename = { ["flake.lock"] = "json" },
})
-- | {{{1 poetry
4 collapsed lines
vim.filetype.add({
filename = { ["poetry.lock"] = "toml" },
})
-- | {{{1 docker
5 collapsed lines
vim.filetype.add({
pattern = { ["[Dd]ockerfile.*"] = "dockerfile" },
})
-- vim: fdm=marker:fen:fdl=0:

Footnotes

  1. I’m aware that enabling modelines is a potential security risk