Implemented folding and some improvements. Moved functions to the utils module

This commit is contained in:
2025-08-29 17:38:23 +02:00
parent d29b2e1012
commit ec9b84ac35
10 changed files with 70 additions and 36 deletions

View File

@@ -1,9 +1,8 @@
-- Save the last file on exit
vim.api.nvim_create_autocmd("VimLeave", {
callback = function()
-- Save the last file path to a file
local last_file = vim.fn.expand('%:p') -- Get the absolute path of the current file
if last_file ~= "" then
if last_file ~= "" then -- The operator means not equal in lua
local file = io.open(vim.fn.stdpath('data') .. "/lastfile.txt", "w")
if file then
file:write(last_file)
@@ -12,3 +11,18 @@ vim.api.nvim_create_autocmd("VimLeave", {
end
end,
})
vim.api.nvim_create_augroup("RememberFolds", {
clear = true
})
vim.api.nvim_create_autocmd({"BufWinLeave"}, {
group = "RememberFolds",
pattern = "*",
command = "silent! mkview",
})
vim.api.nvim_create_autocmd({"BufWinEnter"}, {
group = "RememberFolds",
pattern = "*",
command = "silent! loadview",
})

View File

@@ -33,6 +33,20 @@ vim.keymap.set('n', '<leader>ya', 'ggVG"+y<C-o>')
vim.keymap.set('n', '<leader>ss', ':wa<CR>')
vim.keymap.set("n", "<leader>nn", ":e ~/synced/brainstore/zettelkasten/quick<CR>", { silent = true })
-- Folding
local opts = { noremap = true, silent = true }
local is_all_folded = false
local function toggle_fold()
if is_all_folded then
vim.opt.foldlevel = 99
else
vim.opt.foldlevel = 0
end
is_all_folded = not is_all_folded
end
vim.api.nvim_set_keymap("n", "<leader>ft", "za", opts) -- toggle fold under cursor
vim.keymap.set("n", "<leader>fs", toggle_fold, opts) -- close all folds
-- Quickly open some buffers
vim.keymap.set('n', '<leader>occ', ':e ~/.config/nvim/init.lua<CR>`.zz')
vim.keymap.set('n', '<leader>oct', ':e ~/synced/vault/contacts/contacts.txt<CR>`.zz')

View File

@@ -16,3 +16,13 @@ vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.signcolumn = "yes"
-- Enable Treesitter-based folding
vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
vim.opt.foldlevelstart = 99 -- open all folds by default
vim.opt.fillchars = "fold:╌"
-- 3. Persist folds using mkview/loadview