I would like to modify it so that, if there is solely a single buffer, the current window gets split without prompting the user to select a buffer.
Assistant
Here's a modified version of the configuration that splits the window and shows the current buffer if there's only one buffer, otherwise prompts for buffer selection:
lua
return {
"AstroNvim/astrocore",
opts = {
mappings = {
n = {
["<Left>"] = {
function()
require("astrocore.buffer").nav(-vim.v.count1)
end,
desc = "Previous buffer",
},
["<Right>"] = {
function()
require("astrocore.buffer").nav(vim.v.count1)
end,
desc = "Next buffer",
},
["<Leader>b-"] = {
function()
local buffers = vim.api.nvim_list_bufs()
local valid_buffers = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_option(buf, 'buflisted')
end, buffers)
if #valid_buffers == 1 then
-- If only one buffer, split and use that buffer
vim.cmd.split()
vim.api.nvim_win_set_buf(0, valid_buffers[1])
else
-- Otherwise, use buffer picker
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd.split()
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end,
desc = "Horizontal split buffer from tabline",
},
["<Leader>b\\"] = {
function()
local buffers = vim.api.nvim_list_bufs()
local valid_buffers = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_option(buf, 'buflisted')
end, buffers)
if #valid_buffers == 1 then
-- If only one buffer, split and use that buffer
vim.cmd.vsplit()
vim.api.nvim_win_set_buf(0, valid_buffers[1])
else
-- Otherwise, use buffer picker
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd.vsplit()
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end,
desc = "Vertical split buffer from tabline",
},
["<Leader>b|"] = false,
},
},
},
}
User
Nice, is there an alternative to using the vim.api.nvim_buf_get_option function? It is listed to me as being deprecated.
Assistant
Yes, you can use vim.bo (buffer-local options) instead. Here's the updated configuration using the newer method:
lua
return {
"AstroNvim/astrocore",
opts = {
mappings = {
n = {
["<Left>"] = {
function()
require("astrocore.buffer").nav(-vim.v.count1)
end,
desc = "Previous buffer",
},
["<Right>"] = {
function()
require("astrocore.buffer").nav(vim.v.count1)
end,
desc = "Next buffer",
},
["<Leader>b-"] = {
function()
local buffers = vim.api.nvim_list_bufs()
local valid_buffers = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buflisted
end, buffers)
if #valid_buffers == 1 then
vim.cmd.split()
vim.api.nvim_win_set_buf(0, valid_buffers[1])
else
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd.split()
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end,
desc = "Horizontal split buffer from tabline",
},
["<Leader>b\\"] = {
function()
local buffers = vim.api.nvim_list_bufs()
local valid_buffers = vim.tbl_filter(function(buf)
return vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buflisted
end, buffers)
if #valid_buffers == 1 then
vim.cmd.vsplit()
vim.api.nvim_win_set_buf(0, valid_buffers[1])
else
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd.vsplit()
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end,
desc = "Vertical split buffer from tabline",
},
["<Leader>b|"] = false,
},
},
},
}
The key change is replacing vim.api.nvim_buf_get_option(buf, 'buflisted') with vim.bo[buf].buflisted.
This approach:
Uses the newer, recommended method for accessing buffer-local options
Maintains the same logic as the previous implementation
Checks if buffers are valid and listed before considering them
The key changes are:
I added a check to filter for valid and listed buffers using vim.tbl_filter().
If the number of valid buffers is exactly 1, it splits the window and sets the buffer to that single buffer.
If there are multiple buffers, it falls back to the original buffer picker behavior.
This approach ensures that:
When there's only one buffer, it automatically splits and shows that buffer
When multiple buffers exist, it prompts the user to choose a buffer to split
It only considers valid and listed buffers in the count
The logic is applied to both horizontal (<Leader>b-) and vertical (<Leader>b\) split mappings for consistency.