vimtricks.wiki Concise Vim tricks, one at a time.

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 (like set option+=value)
  • vim.opt.option:prepend(value) — adds a value to the front (like set option^=value)
  • vim.opt.option:remove(value) — removes a value (like set 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.opt over vim.o when modifying list/set/flag options to avoid string-manipulation bugs
  • vim.opt reads back as a Lua table/value, while vim.o returns 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')

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?