๐ŸŒฑ tiny neovim plugin keeping your session safe
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: improve usage

robin 60b7f325 56cbaa6e

+197 -41
+75 -1
README.md
··· 1 - remembers where you left off 1 + # nivvie :seedline: 2 + 3 + tiny neovim session keeper, keeping your place safe 4 + 5 + ## what it does ๐Ÿƒ 6 + 7 + nivvie remembers where you left off and restores it when you return (session 8 + handling). it keeps sessions stored safely and lets you call on them whenever 9 + you need, without clutter or extra ritual. 10 + 11 + ## why nivvie ๐Ÿ‚ 12 + 13 + there are larger session managers with more features. nivvie is a small and 14 + quiet companion, meant for when you want something clean, simple, and reliable. 15 + 16 + ## features ๐ŸŒธ 17 + 18 + - automatic save and restore (no extra steps) 19 + - manual save, load, and delete commands (direct control) 20 + - minimal configuration (defaults just work) 21 + - lightweight and unobtrusive (stays out of the way) 22 + 23 + ## installation ๐ŸŒฟ 24 + 25 + add nivvie with your favourite plugin manager. 26 + 27 + ###### `vim.pack` 28 + 29 + ```lua 30 + vim.pack.add({ "comfysage/nivvie.nvim" }) 31 + ``` 32 + 33 + ###### `lazy.nvim` 34 + 35 + ```lua 36 + { 37 + "comfysage/nivvie.nvim", 38 + lazy = false, -- nivvie takes care of its own lazy loading 39 + config = function() 40 + require("nivvie").setup() 41 + end 42 + } 43 + ``` 44 + 45 + ## usage ๐ŸŒผ 46 + 47 + sessions are saved automatically when you quit neovim and restored when you open it again. you can also manage sessions yourself: 48 + 49 + ``` 50 + :Nivvie save [name] " save the current session for the current directory or with a custom name 51 + :Nivvie load [name] " load session for the current directory or with a custom name 52 + :Nivvie delete [name] " delete session for the current directory or with a custom name 53 + ``` 54 + 55 + you may also map keys to these commands: 56 + 57 + ```lua 58 + vim.keymap.set("n", "<leader>ss", ":NivvieSave<CR>") 59 + vim.keymap.set("n", "<leader>sl", ":NivvieLoad<CR>") 60 + ``` 61 + 62 + ## configuration ๐ŸŒพ 63 + 64 + nivvie works without configuration (zero setup). you can still adjust its behavior: 65 + 66 + ```lua 67 + require("nivvie").setup({ 68 + -- where sessions are stored 69 + session_dir = vim.fn.stdpath("state") .. "/sessions", 70 + -- save on exit 71 + autosave = true, 72 + -- load on start 73 + autorestore = true, 74 + }) 75 + ```
+122 -40
plugin/nivvie.lua
··· 1 1 if vim.g.loaded_session then 2 - return 2 + return 3 3 end 4 4 5 5 vim.g.loaded_session = true 6 6 7 7 local session = {} 8 8 9 + ---@private 10 + session.stdin = false 11 + 9 12 function session.get_uri() 10 - return string.gsub(vim.fn.getcwd(), "[^a-zA-Z0-9_.-]", function(s) 11 - return "<" .. vim.fn.char2nr(s) 12 - end) 13 + return string.gsub(vim.fn.getcwd(), '[^a-zA-Z0-9_.-]', function(s) 14 + return '<' .. vim.fn.char2nr(s) 15 + end) 13 16 end 14 17 15 - local sessiondir = vim.g.sessiondir or vim.fs.joinpath(vim.fn.stdpath("state"), "sessions") 18 + local sessiondir = vim.g.sessiondir 19 + or vim.fs.joinpath(vim.fn.stdpath 'state', 'sessions') 16 20 17 - function session.get_path() 18 - return vim.fs.joinpath(sessiondir, session.get_uri() .. ".vim") 21 + ---@param name? string 22 + function session.get_path(name) 23 + return vim.fs.joinpath(sessiondir, (name or session.get_uri()) .. '.vim') 19 24 end 20 25 21 - function session.save() 22 - local session_file = session.get_path() 26 + ---@param name? string 27 + function session.save(name) 28 + local session_file = session.get_path(name) 23 29 24 - vim.fn.mkdir(sessiondir, "p") 30 + vim.fn.mkdir(sessiondir, 'p') 25 31 26 - vim.api.nvim_cmd({ 27 - cmd = "mksession", 28 - bang = true, 29 - args = { session_file }, 30 - }, {}) 32 + vim.api.nvim_cmd({ 33 + cmd = 'mksession', 34 + bang = true, 35 + args = { session_file }, 36 + }, {}) 31 37 end 32 38 33 39 function session.clean() 34 - vim.iter(ipairs(vim.api.nvim_list_bufs())):each(function(_, bufnr) 35 - if vim.bo[bufnr].buftype ~= "" then 36 - vim.api.nvim_buf_delete(bufnr, { force = true }) 37 - end 38 - end) 40 + vim.iter(ipairs(vim.api.nvim_list_bufs())):each(function(_, bufnr) 41 + if vim.bo[bufnr].buftype ~= '' then 42 + vim.api.nvim_buf_delete(bufnr, { force = true }) 43 + end 44 + end) 39 45 end 40 46 41 - function session.load() 42 - local path = session.get_path() 43 - if vim.uv.fs_stat(path) then 44 - vim.api.nvim_cmd({ 45 - cmd = "source", 46 - args = { path }, 47 - }, {}) 48 - vim.api.nvim_exec_autocmds("SessionLoadPost", {}) 49 - end 47 + ---@param name? string 48 + function session.load(name) 49 + local path = session.get_path(name) 50 + if vim.uv.fs_stat(path) then 51 + vim.api.nvim_cmd({ 52 + cmd = 'source', 53 + args = { path }, 54 + }, {}) 55 + vim.api.nvim_exec_autocmds('SessionLoadPost', {}) 56 + end 50 57 end 51 58 52 - local group = vim.api.nvim_create_augroup("nivvie", { clear = true }) 59 + ---- 53 60 54 - vim.api.nvim_create_autocmd({ "VimLeavePre" }, { 55 - group = group, 56 - callback = function() 57 - session.clean() 58 - session.save() 59 - end, 60 - }) 61 + function session.shouldload() 62 + if session.stdin then 63 + return false 64 + end 61 65 62 - if vim.fn.argc() > 0 then 63 - return 66 + if vim.fn.argc() > 0 then 67 + return false 68 + end 69 + 70 + local bufs = vim.api.nvim_list_bufs() 71 + bufs = vim 72 + .iter(bufs) 73 + :filter(function(buf) 74 + return vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == '' 75 + end) 76 + :totable() 77 + 78 + return #bufs == 0 64 79 end 80 + 81 + --- only load if necessary 82 + function session.autoload() 83 + if not session.shouldload() then 84 + return 85 + end 86 + session.load() 87 + end 88 + 89 + local group = vim.api.nvim_create_augroup('nivvie', { clear = true }) 90 + 91 + vim.api.nvim_create_autocmd({ 'VimLeavePre' }, { 92 + group = group, 93 + callback = function() 94 + session.clean() 95 + session.save() 96 + end, 97 + }) 98 + 99 + vim.api.nvim_create_autocmd({ 'StdinReadPost' }, { 100 + group = group, 101 + callback = function() 102 + session.stdin = true 103 + end, 104 + }) 105 + 65 106 vim.schedule(function() 66 - session.load() 107 + vim.api.nvim_create_user_command('Nivvie', function(args) 108 + if args.fargs[1] == 'save' then 109 + session.save(args.fargs[2]) 110 + return 111 + elseif args.fargs[1] == 'load' then 112 + session.load(args.fargs[2]) 113 + return 114 + elseif args.fargs[1] == 'delete' then 115 + local path = session.get_path(args.fargs[2]) 116 + if vim.uv.fs_stat(path) then 117 + vim.uv.fs_unlink(path) 118 + end 119 + end 120 + end, { 121 + complete = function() 122 + return { 123 + 'save', 124 + 'load', 125 + 'delete', 126 + } 127 + end, 128 + }) 67 129 end) 130 + 131 + ---- 132 + 133 + if vim.v.vim_did_enter > 0 then 134 + vim.schedule(function() 135 + session.autoload() 136 + end) 137 + return 138 + end 139 + 140 + vim.api.nvim_create_autocmd({ 'VimEnter' }, { 141 + group = group, 142 + callback = function() 143 + vim.schedule(function() 144 + session.autoload() 145 + end) 146 + end, 147 + }) 148 + 149 + return session