bug: Can't restore buffer order with pre_save hook
Did you check the docs and existing issues?
- [X] I have read the docs
- [X] I have searched the existing issues
Neovim version (nvim -v)
NVIM v0.10.0
Operating system/version
NixOS 24.11.20240704.a6f2adf (Vicuna) x86_64
Describe the bug
I'm using barbar as a buffer tabline and I want to preserve the buffer order when reloading the session. According to the plugin's documentation:
barbar.nvimcan restore the order that your buffers were in, as well as whether a buffer was pinned. To do this,sessionoptionsmust containglobals, and theUser SessionSavePreevent must be executed before:mksession.
Now, following resession's documentation, I did this using the pre_save hook like so:
{
'stevearc/resession.nvim',
config = true,
init = function()
vim.opt.sessionoptions:append 'globals'
require('resession').add_hook('pre_save', function()
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
end)
end,
},
I've set up resession to auto load the session on login by following the Create one session per directory guide, but when the session auto-loads, the buffer order is reset to before I re-ordered them.
I also tried to use ResessionSavePre autocmd, but it's the same case:
vim.opt.sessionoptions:append 'globals'
vim.api.nvim_create_autocmd({ 'User' }, {
pattern = 'ResessionSavePre',
callback = function()
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
end,
})
I really want to switch to resession since it's light and fast, but I also want to be able to re-order my buffers.
What is the severity of this bug?
minor (annoyance)
Steps To Reproduce
- nvim -u repro.lua
- Open at least two buffers:
:e file1.txt,:e file2.txt, ... - Switch their order using
Alt-Shift-JorAlt-Shift-K - Quit neovim
- nvim -u repro.lua
Expected Behavior
After reordering the buffers, the next time the session auto loads the buffers are loaded in their new positions.
Directory structure
./file1.txt ./file2.txt ./repro.lua
Repro
local root = vim.fn.fnamemodify('./.repro', ':p')
for _, name in ipairs { 'config', 'data', 'state', 'runtime', 'cache' } do
vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end
-- bootstrap lazy
local lazypath = root .. '/plugins/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
vim.fn.system {
'git',
'clone',
'--filter=blob:none',
'--single-branch',
'https://github.com/folke/lazy.nvim.git',
lazypath,
}
end
vim.opt.runtimepath:prepend(lazypath)
-- install plugins
local plugins = {
'folke/tokyonight.nvim',
{
'stevearc/resession.nvim',
config = true,
init = function()
vim.opt.sessionoptions:append 'globals'
require('resession').add_hook('pre_save', function()
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
end)
end,
},
-- extra plugins
{
'romgrk/barbar.nvim',
config = true,
event = 'BufEnter',
dependencies = {
-- optional
'lewis6991/gitsigns.nvim', -- git status
'nvim-tree/nvim-web-devicons', -- file icons
},
init = function()
vim.g.barbar_auto_setup = false
local map = vim.api.nvim_set_keymap
local opts = function(desc)
return { desc = desc, noremap = true, silent = true }
end
-- Move to previous/next
map('n', '<A-j>', '<Cmd>BufferPrevious<CR>', opts 'Go to Previous Buffer')
map('n', '<A-k>', '<Cmd>BufferNext<CR>', opts 'Go to Next Buffer')
-- Re-order to previous/next
map('n', '<A-S-j>', '<Cmd>BufferMovePrevious<CR>', opts 'Move Buffer Left')
map('n', '<A-S-k>', '<Cmd>BufferMoveNext<CR>', opts 'Move Buffer Right')
end,
version = '^1.0.0', -- optional: only update when a new 1.x version is released
},
}
require('lazy').setup(plugins, {
root = root .. '/plugins',
})
vim.cmd.colorscheme 'tokyonight'
-- Create one session per directory
vim.api.nvim_create_autocmd('VimEnter', {
callback = function()
-- Only load the session if nvim was started with no args
if vim.fn.argc(-1) == 0 then
-- Save these to a different directory, so our manual sessions don't get polluted
require('resession').load(vim.fn.getcwd(), { dir = 'dirsession', silence_errors = true })
end
end,
nested = true,
})
vim.api.nvim_create_autocmd('VimLeavePre', {
callback = function()
require('resession').save(vim.fn.getcwd(), { dir = 'dirsession', notify = false })
end,
})
Did you check the bug with a clean config?
- [X] I have confirmed that the bug reproduces with
nvim -u repro.luausing the repro.lua file above.