How do I add or remove individual values from a comma-separated Vim option using Neovim Lua?
Answer
vim.opt.option:append(value)
Explanation
Neovim's vim.opt namespace exposes options as objects with methods for safely adding or removing individual values without the string concatenation errors that come from direct vim.o assignment. This is the idiomatic Lua equivalent of Vimscript's set option+=value and set option-=value.
How it works
vim.opt.option:append(value)— adds a value to the end (likeset option+=value)vim.opt.option:prepend(value)— adds a value to the front (likeset option^=value)vim.opt.option:remove(value)— removes a value (likeset option-=value)vim.opt.option = value— overwrites the option entirely
Using :append() is safer than string concatenation because it avoids inadvertently duplicating separators or malforming the option value.
Example
-- Extend the runtime path
vim.opt.runtimepath:append('~/.config/nvim/after')
-- Reduce noisy completion messages
vim.opt.shortmess:append('c')
-- Exclude hyphens from keyword characters
vim.opt.iskeyword:remove('-')
-- Set completeopt as a list (direct assignment)
vim.opt.completeopt = { 'menu', 'menuone', 'noselect' }
Tips
- Prefer
vim.optovervim.owhen modifying list/set/flag options to avoid string-manipulation bugs vim.optreads back as a Lua table/value, whilevim.oreturns the raw string- Use
print(vim.inspect(vim.opt.rtp:get()))to debug the current value as a Lua table - This API is Neovim-specific; for Vimscript compatibility use
vim.cmd('set option+=value')