folke / persistence.nvim

💾 Simple session management for Neovim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feature: don't save buffers out of the cwd

idr4n opened this issue · comments

Did you check the docs?

  • I have read all the persistence.nvim docs

Is your feature request related to a problem? Please describe.

Yes, kind of. Buffers with files that are not part of the cwd seem to be persistent even after closing them.

Describe the solution you'd like

At the moment (using LazyVim distro) buffers with files that are not part of the cwd are saved in the session. Furthermore, those buffers seem to be persistent, that is, even if I close them, quit neovim, reopen and restore the session, they show up again.

Describe alternatives you've considered

I have consider implementing a function to pass to pre_save that close/delete the buffers with file paths that are no children of the cwd. Would this be the approach to go with here?

Thanks in advance!

Additional context

This is a session which cwd is ~/.config/nvim:

let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
silent tabonly
cd ~/.config/nvim
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
  let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
  set shortmess=aoOA
else
  set shortmess=aoO
endif
badd +16 lua/plugins/colorschemes.lua
badd +1 lua/config/options.lua
badd +0 ~/.prettierrc.json
argglobal
%argdel
$argadd ~/.prettierrc.json
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
  silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
  exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

As you can see, there is ~/.prettierrc.json that was saved and it is not part of the cwd.

@idr4n The pre_save function you described is a valid solution, which would look like this:

local function pre_save()
	-- remove buffers whose files are located outside of cwd
	local cwd = vim.fn.getcwd() .. '/'
	for _, buf in ipairs(vim.api.nvim_list_bufs()) do
		local bufpath = vim.api.nvim_buf_get_name(buf) .. '/'
		if not bufpath:match('^' .. vim.pesc(cwd)) then
			vim.api.nvim_buf_delete(buf, {})
		end
	end
end

require('persistence').setup{
	pre_save = pre_save,
}

Thanks @koonix. I'm closing this issue based on your solution.