How do I set Vim options from a Lua config in Neovim using vim.opt?
Answer
vim.opt
Explanation
Neovim's vim.opt interface provides a Lua-native way to set Vim options that mirrors how :set works in Vimscript — including support for appending, prepending, and removing values from list-style options. It is the recommended approach for init.lua configs over vim.o, which provides raw string access without the list helpers.
How it works
vim.opt.option = value— equivalent to:set option=valuevim.opt.option:append(value)— equivalent to:set option+=valuevim.opt.option:prepend(value)— equivalent to:set option^=valuevim.opt.option:remove(value)— equivalent to:set option-=valuevim.opt_local.option— sets a buffer-local option (like:setlocal)vim.opt_global.option— sets the global value (like:setglobal)
Example
" Vimscript
set expandtab tabstop=2 shiftwidth=2
set completeopt-=preview
set path+=src/**
-- Equivalent Lua
vim.opt.expandtab = true
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.completeopt:remove('preview')
vim.opt.path:append('src/**')
Tips
vim.o.optionis a simpler string/boolean accessor but does not support:append()or:remove()— usevim.optwhen working with comma-separated options likecompleteopt,path, orwildignore- Read a value back with
vim.opt.option:get()which returns a Lua table for list options vim.wo(window) andvim.bo(buffer) are shorthand for setting window- and buffer-local scopes respectively, thoughvim.opt_localis more explicit