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

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=value
  • vim.opt.option:append(value) — equivalent to :set option+=value
  • vim.opt.option:prepend(value) — equivalent to :set option^=value
  • vim.opt.option:remove(value) — equivalent to :set option-=value
  • vim.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.option is a simpler string/boolean accessor but does not support :append() or :remove() — use vim.opt when working with comma-separated options like completeopt, path, or wildignore
  • Read a value back with vim.opt.option:get() which returns a Lua table for list options
  • vim.wo (window) and vim.bo (buffer) are shorthand for setting window- and buffer-local scopes respectively, though vim.opt_local is more explicit

Next

How do I encode and decode JSON data in Vimscript for configuration and plugin development?