Search code examples
luaeditorneovim

Disabling buffer-specific swap file creation in Neovim using Lua


How can one disable the creation of swap files (.swp) for a specific buffer in Neovim using Lua?

The intended use case is to create a "scratch" buffer that can be used for holding the output of certain commands. This output is not critical nor does it need to persist, hence a .swp file is not needed.


Solution

  • To disable swap files for a specific buffer in Neovim, one can use:

    local bufnr = vim.api.nvim_get_current_buf()
    vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
    

    If one is looking to permanently disable swap file creation in Neovim (generally not recommended), add the following line to your configuration file:

    vim.opt.swapfile = false