I’m trying to run :lua require'alpha'.start(false)
| bd! #, but Neovim recognizes the second argument as more Lua. Is there any way to add an end marker to the Lua section with it remaining a one-liner?
Consider an alternative where you stay in lua and just execute vimscript commands like this:
lua require('alpha').start(false) vim.api.nvim_command('bd')
NOTE: in some cases you might want to use vim.cmd
, make sure you read about both of them
This worked, but I have a new problem: I need this to work within an if statement. My full function is
:if len(getbufinfo({"buflisted":1})) > 1 | bp | bd! # | else | lua require("alpha").start(false) | bd! # | endif
Even if I enclose the bd! # within Lua, the statement fails because it needs to close the if.
Why not just use multiple lines?
I’m trying to bind the command to a keybind. I’m trying to convert the entire thing to a Lua function right now. Edit: Is there any way to get the total number of buffers in Lua? If I can do that, I can most likely rewrite the entire function in Lua.
You can use #vim.fn.getbufinfo({buflisted = 1})
Also, :execute
can be used for this: :execute 'lua require("alpha").start(false)'
The Lua function is acting strange, but using :execute makes the Vimscript work. Thanks!
Can you show how you’re trying to bind? Maybe there is a better way.
The Vimscript way:
vim.api.nvim_set_keymap("n", "gbk", [[:if len(getbufinfo({"buflisted":1})) > 1 | bp | else | execute "lua require('alpha').start(false)" | endif | bd! #<CR>]], {noremap = true, silent = true})
It’s ugly, but it works.
The Lua way:
local bufferCount = vim.fn.len(vim.fn.getbufinfo({buflisted = 1}))
function _G.smartBufferDelete()
if bufferCount > 1 then
return vim.cmd("bp") and vim.cmd("bd! #")
else
return require("alpha").start(false) and vim.cmd("bd! #")
end
end
vim.api.nvim_set_keymap("n", "gbk", "v:lua.smartBufferDelete()", {noremap = true, silent = true})
This one doesn’t work. I’m not entirely sure why, but I’m not entirely sure what I’m doing either.