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”
set foldmethod=markerset foldenableset foldlevel=0
3. Place your markers
Implied level | Style |
---|---|
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:
command | Function |
---|---|
zj | moves the cursor to the next fold. |
zk | moves the cursor to the previous fold. |
zM | closes all open folds. |
zR | decreases 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:
-- {{{1 Disable autoformat for some filetypes7 collapsed lines
vim.api.nvim_create_autocmd({ "FileType" }, { pattern = { "python", "toml" }, callback = function() vim.b.autoformat = false end,})
-- {{{1 Add new filetype patterns1 collapsed line
-- | {{{1 nix4 collapsed lines
vim.filetype.add({ filename = { ["flake.lock"] = "json" },})
-- | {{{1 poetry4 collapsed lines
vim.filetype.add({ filename = { ["poetry.lock"] = "toml" },})
-- | {{{1 docker5 collapsed lines
vim.filetype.add({ pattern = { ["[Dd]ockerfile.*"] = "dockerfile" },})
-- vim: fdm=marker:fen:fdl=0:
Footnotes
-
I’m aware that enabling modelines is a potential security risk ↩