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

How do I prevent Vim's write strategy from breaking LSP file watchers by changing a file's inode on save?

Answer

:set nowritebackup

Explanation

By default, Vim saves files using a "write-then-rename" strategy: it writes to a temporary backup file and then renames it over the original. This causes the saved file to get a new inode number, which breaks file system watchers — the mechanism many LSP servers use to detect when files change on disk.

Setting nowritebackup tells Vim to write directly to the original file, preserving the inode and keeping LSP servers in sync.

How it works

Default behavior (writebackup on):

  1. Vim writes buffer content to filename~ (temporary backup)
  2. Renames filename~filename (new inode)
  3. LSP file watcher sees a new file — loses its watch on the old inode

With nowritebackup:

  1. Vim writes directly to filename in place
  2. Inode unchanged — file watcher events fire correctly

The companion option backup controls whether a permanent filename~ backup is kept after saving. These two are independent:

set nowritebackup  " write in place (good for LSP)
set nobackup       " don't keep a backup copy after saving

Example

In your init.vim or init.lua for any setup using nvim-lspconfig or the built-in LSP client:

" Vimscript
set nobackup
set nowritebackup
-- Lua
vim.opt.backup = false
vim.opt.writebackup = false

Tips

  • The Neovim LSP documentation recommends both nobackup and nowritebackup for LSP servers that use file watching (e.g., gopls, rust-analyzer, typescript-language-server)
  • If you rely on backup files for safety, configure undofile instead: :set undofile gives persistent undo without the inode-changing write strategy
  • On network file systems or editors that can crash, losing writebackup is a trade-off — consider undodir as a safety net

Next

How do I enable matchit so % jumps between if/else/end style pairs?