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):
- Vim writes buffer content to
filename~(temporary backup) - Renames
filename~→filename(new inode) - LSP file watcher sees a new file — loses its watch on the old inode
With nowritebackup:
- Vim writes directly to
filenamein place - 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
nobackupandnowritebackupfor LSP servers that use file watching (e.g.,gopls,rust-analyzer,typescript-language-server) - If you rely on backup files for safety, configure
undofileinstead::set undofilegives persistent undo without the inode-changing write strategy - On network file systems or editors that can crash, losing
writebackupis a trade-off — considerundodiras a safety net