You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Telescope extension may be opened via `:Telescope persisted`. The available actions are:
131
+
The Telescope extension may be opened via `:Telescope persisted`. The default actions are:
132
132
133
133
-`<CR>` - Open/source the session file
134
134
-`<C-b>` - Add/update the git branch for the session file
@@ -140,8 +140,8 @@ The Telescope extension may be opened via `:Telescope persisted`. The available
140
140
The plugin sets a number of global variables throughout its lifecycle:
141
141
142
142
-`vim.g.persisting` - (bool) Determines if the plugin is active for the current session
143
-
-`vim.g.persisted_exists` - (bool) Determines if a session exists for the current working directory
144
-
-`vim.g.persisted_loaded_session` - (string) The file path to the current session
143
+
-`vim.g.persisting_session` - (string) The file path to the current session (if `follow_cwd` is false)
144
+
-`vim.g.persisted_loaded_session` - (string) The file path to the last loaded session
145
145
146
146
## :wrench: Configuration
147
147
@@ -150,42 +150,49 @@ The plugin sets a number of global variables throughout its lifecycle:
150
150
The plugin comes with the following defaults:
151
151
152
152
```lua
153
-
require("persisted").setup({
154
-
log_level="ERROR", -- One of "TRACE", "DEBUG", "ERROR"
155
-
save_dir=vim.fn.expand(vim.fn.stdpath("data") .."/sessions/"), -- directory where session files are saved
156
-
silent=false, -- silent nvim message when sourcing session file
157
-
use_git_branch=false, -- create session files based on the branch of a git enabled repository
158
-
default_branch="main", -- the branch to load if a session file is not found for the current branch
159
-
autosave=true, -- automatically save session files when exiting Neovim
160
-
should_autosave=nil, -- function to determine if a session should be autosaved
161
-
autoload=false, -- automatically load the session for the cwd on Neovim startup
162
-
on_autoload_no_session=nil, -- function to run when `autoload = true` but there is no session to load
163
-
follow_cwd=true, -- change session file name to match current working directory if it changes
164
-
allowed_dirs=nil, -- table of dirs that the plugin will auto-save and auto-load from
165
-
ignored_dirs=nil, -- table of dirs that are ignored when auto-saving and auto-loading
166
-
ignored_branches=nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
153
+
{
154
+
autostart=true, -- Automatically start the plugin on load?
155
+
156
+
-- Function to determine if a session should be saved
157
+
---@typefun(): boolean
158
+
should_save=function()
159
+
returntrue
160
+
end,
161
+
162
+
save_dir=vim.fn.expand(vim.fn.stdpath("data") .."/sessions/"), -- Directory where session files are saved
163
+
164
+
follow_cwd=true, -- Change the session file to match any change in the cwd?
165
+
use_git_branch=false, -- Include the git branch in the session file name?
166
+
autoload=false, -- Automatically load the session for the cwd on Neovim startup?
167
+
168
+
-- Function to run when `autoload = true` but there is no session to load
169
+
---@typefun(): any
170
+
on_autoload_no_session=function() end,
171
+
172
+
allowed_dirs= {}, -- Table of dirs that the plugin will start and autoload from
173
+
ignored_dirs= {}, -- Table of dirs that are ignored for starting and autoloading
174
+
167
175
telescope= {
168
-
reset_prompt=true, -- Reset the Telescope prompt after an action?
169
-
mappings= { -- table of mappings for the Telescope extension
170
-
change_branch="<c-b>",
171
-
copy_session="<c-c>",
172
-
delete_session="<c-d>",
176
+
mappings= { -- Mappings for managing sessions in Telescope
177
+
copy_session="<C-c>",
178
+
change_branch="<C-b>",
179
+
delete_session="<C-d>",
173
180
},
174
-
icons= { -- icons displayed in the picker, set to nil to disable entirely
181
+
icons= { -- icons displayed in the Telescope picker
182
+
selected=" ",
183
+
dir=" ",
175
184
branch=" ",
176
-
dir=" ",
177
-
selected=" ",
178
185
},
179
186
},
180
-
})
187
+
}
181
188
```
182
189
183
190
**What is saved in the session?**
184
191
185
192
As the plugin uses Vim's `:mksession` command then you may change the `vim.o.sessionoptions` value to determine what to write into the session. Please see `:h sessionoptions` for more information.
186
193
187
194
> [!NOTE]
188
-
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,tabpages,winpos,winsize"`
195
+
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize"`
By default, the plugin will automatically save a Neovim session to disk when the `VimLeavePre` event is triggered. Autosaving can be turned off by:
222
+
By default, the plugin will automatically start when the setup function is called. This results in a Neovim session being saved to disk when the `VimLeavePre` event is triggered. This can be disabled by:
216
223
217
224
```lua
218
225
require("persisted").setup({
219
-
autosave=false,
226
+
autostart=false,
220
227
})
221
228
```
222
229
223
-
Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
230
+
Autostarting can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
231
+
232
+
**`should_save`**
224
233
225
-
There may be occasions when you do not wish to autosave; perhaps when a dashboard or a certain buftype is present. To control this, a callback function, `should_autosave`, may be used which should return a boolean value.
234
+
There may be occasions when you do not wish to save the session; perhaps when a dashboard or a certain filetype is present. To handle this, the `should_save` function may be used which should return a boolean value.
226
235
227
236
```lua
228
237
require("persisted").setup({
229
-
should_autosave=function()
230
-
-- do not autosave if the alpha dashboard is the current filetype
238
+
---@returnbool
239
+
should_save=function()
240
+
-- Do not save if the alpha dashboard is the current filetype
231
241
ifvim.bo.filetype=="alpha" then
232
242
returnfalse
233
243
end
@@ -236,10 +246,7 @@ require("persisted").setup({
236
246
})
237
247
```
238
248
239
-
Of course, if you wish to manually save the session when autosaving is disabled, the `:SessionSave` command can be used.
240
-
241
-
> [!NOTE]
242
-
> If `autosave = false` then the `should_autosave` callback will not be executed.
249
+
Of course, if you wish to manually save the session the `:SessionSave` command can be used.
243
250
244
251
**Autoloading**
245
252
@@ -251,7 +258,7 @@ require("persisted").setup({
251
258
})
252
259
```
253
260
254
-
You can also provide a function to run when `autoload = true`but there is no session to be loaded:
261
+
You can also provide a function to run when `autoload = true`and there is no session to load:
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
266
-
267
-
> [!NOTE]
268
-
> Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will.
269
-
270
-
**Following current working directory**
271
-
272
-
There may be a need to change the working directory to quickly access files in other directories without changing the current session's name on save. This behavior can be configured with `follow_cwd = false`.
273
-
274
-
By default, the session name will match the current working directory:
275
-
276
-
```lua
277
-
require("persisted").setup({
278
-
follow_cwd=true,
279
-
})
280
-
```
272
+
Autoloading can be further controlled for directories in the `allowed_dirs` and `ignored_dirs` config tables.
281
273
282
-
> [!NOTE]
283
-
> If `follow_cwd = false`the session name is stored upon loading under the global variable `vim.g.persisting_session`. This variable can be manually adjusted if changes to the session name are needed. Alternatively, if `follow_cwd = true` then `vim.g.persisting_session = nil`.
274
+
> [!IMPORTANT]
275
+
> By design, the plugin will not autoload a session when any arguments are passed to Neovim such as `nvim my_file.py`
284
276
285
277
**Allowed directories**
286
278
287
-
You may specify a table of directories for which the plugin will autosave and/or autoload from. For example:
279
+
You may specify a table of directories for which the plugin will start and/or autoload from. For example:
Specifying `~/Code` will autosave and autoload from that directory as well as all its sub-directories.
290
+
Specifying `~/Code` will start and autoload from that directory as well as all its sub-directories.
299
291
300
292
> [!NOTE]
301
-
> If `allowed_dirs` is left at its default value and `autosave` and/or `autoload` are set to `true`, then the plugin will autoload/autosave from _any_ directory
293
+
> If `allowed_dirs` is left at its default value and `autostart` and/or `autoload` are set to `true`, then the plugin will start and autoload from _any_ directory
302
294
303
295
**Ignored directories**
304
296
305
-
You may specify a table of directories for which the plugin will **never**autosave and autoload from. For example:
297
+
You may specify a table of directories for which the plugin will **never**start and autoload from. For example:
306
298
307
299
```lua
308
300
require("persisted").setup({
@@ -330,19 +322,6 @@ require("persisted").setup({
330
322
331
323
In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.
332
324
333
-
**Ignored branches**
334
-
335
-
You may specify a table of patterns that match against branches for which the plugin will **never** autosave and autoload from:
336
-
337
-
```lua
338
-
require("persisted").setup({
339
-
ignored_branches= {
340
-
"^master",
341
-
"feature/%u"
342
-
},
343
-
})
344
-
```
345
-
346
325
**Events / Callbacks**
347
326
348
327
The plugin fires events at various points during its lifecycle:
@@ -355,21 +334,19 @@ The plugin fires events at various points during its lifecycle:
355
334
-`PersistedSavePost` - For _after_ a session is saved
356
335
-`PersistedDeletePre` - For _before_ a session is deleted
357
336
-`PersistedDeletePost` - For _after_ a session is deleted
358
-
-`PersistedStateChange` - For when a session is _started_ or _stopped_
359
-
-`PersistedToggled` - For when a session is toggled
337
+
-`PersistedStart` - For when a session has _started_
338
+
-`PersistedStop` - For when a session has _stopped_
339
+
-`PersistedToggle` - For when a session is toggled
360
340
361
341
These events can be consumed anywhere within your configuration by utilising the `vim.api.nvim_create_autocmd` function.
362
342
363
343
A commonly requested example is to use the Telescope extension to load a session, saving the current session before clearing all of the open buffers:
To consume this data, use the `session.data` table in your autocmd:
366
+
## :building_construction: Extending the Plugin
367
+
368
+
The plugin has been designed to be fully extensible. All of the functions in the [init.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/init.lua) and [utils.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/utils.lua) file are public.
369
+
370
+
Consider a user who wishes to autoload a session if arguments are passed to Neovim. A custom autocmd can be created which forces the autoload:
395
371
396
372
```lua
397
-
vim.api.nvim_create_autocmd({ "User" }, {
398
-
pattern="PersistedLoadPost",
399
-
group=group,
400
-
callback=function(session)
401
-
print(session.data.branch)
373
+
localpersisted=require("persisted")
374
+
375
+
persisted.setup({
376
+
autoload=true
377
+
})
378
+
379
+
vim.api.nvim_create_autocmd("VimEnter", {
380
+
nested=true,
381
+
callback=function()
382
+
-- Add more complex logic here
383
+
ifvim.fn.argc() >0then
384
+
-- Leverage the plugin's ability to resolve allowed_dirs and ignored_dirs
385
+
require("persisted").autoload({ force=true })
386
+
end
402
387
end,
403
388
})
404
389
```
405
390
406
-
> [!NOTE]
407
-
> This data is available for the `PersistedLoad`, `PersistedDelete` and `PersistedTelescope` events
391
+
Or, a user who wishes to check whether the current branch is in a table of branches to be ignored:
0 commit comments