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
Modified Astrocore Configuration
Here's the updated configuration that checks if there's only one buffer open and splits the window without prompting the user to select a buffer:
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.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_loaded(bufnr) and vim.api.nvim_buf_get_option(bufnr, "buflisted")
end, vim.api.nvim_list_bufs())
if #buffers == 1 then
vim.cmd.split()
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.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_loaded(bufnr) and vim.api.nvim_buf_get_option(bufnr, "buflisted")
end, vim.api.nvim_list_bufs())
if #buffers == 1 then
vim.cmd.vsplit()
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,
},
},
},
}
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, vim.api.nvim_buf_get_option is deprecated.
The recommended replacement is vim.api.nvim_get_option_value. Here's how you can modify the split_with_buffer_picker function to use the new API:
lua
local function split_with_buffer_picker(split_cmd)
local buffers = vim.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_loaded(bufnr) and vim.api.nvim_get_option_value("buflisted", { buf = bufnr })
end, vim.api.nvim_list_bufs())
if #buffers == 1 then
vim.cmd(split_cmd)
else
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd(split_cmd)
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end
We pass a table with { buf = bufnr } as the second argument to nvim_get_option_value, specifying the buffer for which we want to retrieve the option value.
This change ensures your code stays compatible with future Neovim versions.
Note: The same deprecation applies to other nvim_buf_get_option and nvim_win_get_option functions. You can replace them with nvim_get_option_value using the following pattern:
The full updated Astrocore configuration with the non-deprecated function would be:
What's Changed?
We added a check for the number of loaded and listed buffers using vim.api.nvim_list_bufs() and filtering them with vim.tbl_filter(). If there's only one buffer (#buffers == 1), we simply split the window using vim.cmd.split() or vim.cmd.vsplit() without calling the buffer picker. Otherwise, we fall back to the original behavior of opening the buffer picker.
Extracting a Function for Reusability
To avoid duplicating the buffer count logic, you can extract it into a separate function:
lua
local function split_with_buffer_picker(split_cmd)
local buffers = vim.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_loaded(bufnr) and vim.api.nvim_buf_get_option(bufnr, "buflisted")
end, vim.api.nvim_list_bufs())
if #buffers == 1 then
vim.cmd(split_cmd)
else
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd(split_cmd)
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end
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() split_with_buffer_picker("split") end,
desc = "Horizontal split buffer from tabline",
},
["<Leader>b\\"] = {
function() split_with_buffer_picker("vsplit") end,
desc = "Vertical split buffer from tabline",
},
["<Leader>b|"] = false,
},
},
},
}
This refactored version is more concise and easier to maintain. The split_with_buffer_picker function takes a split_cmd argument, which allows you to reuse the logic for both horizontal and vertical splits.
lua
local function split_with_buffer_picker(split_cmd)
local buffers = vim.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_loaded(bufnr) and vim.api.nvim_get_option_value("buflisted", { buf = bufnr })
end, vim.api.nvim_list_bufs())
if #buffers == 1 then
vim.cmd(split_cmd)
else
require("astroui.status.heirline").buffer_picker(function(bufnr)
vim.cmd(split_cmd)
vim.api.nvim_win_set_buf(0, bufnr)
end)
end
end
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() split_with_buffer_picker("split") end,
desc = "Horizontal split buffer from tabline",
},
["<Leader>b\\"] = {
function() split_with_buffer_picker("vsplit") end,
desc = "Vertical split buffer from tabline",
},
["<Leader>b|"] = false,
},
},
},
}