From fcf752476a33a26058b3342ac09108f76801bd4b Mon Sep 17 00:00:00 2001 From: glepnir Date: Sun, 28 Sep 2025 11:28:05 +0800 Subject: [PATCH] fix(cmd): :update writes new file buffers only for real files #35939 Problem: :update should write new file buffers, but previous fix affected special buffer types (acwrite, nofile, etc.). Solution: Add bt_nofilename() check to only write new files for buffers representing real filesystem paths. --- runtime/doc/editing.txt | 3 ++- runtime/doc/vim_diff.txt | 1 + src/nvim/ex_cmds.c | 4 +++- test/functional/ex_cmds/write_spec.lua | 11 +++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/runtime/doc/editing.txt b/runtime/doc/editing.txt index 7d1798c34e..cb5de0f2e5 100644 --- a/runtime/doc/editing.txt +++ b/runtime/doc/editing.txt @@ -989,7 +989,8 @@ slower (but safer). *:up* *:update* :[range]up[date][!] [++opt] [>>] [file] Like ":write", but only write when the buffer has been - modified. + modified, or when the buffer represents a new file + that doesn't exist on disk. WRITING WITH MULTIPLE BUFFERS *buffer-write* diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index d0c24e296a..470378d87e 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -308,6 +308,7 @@ Commands: - |:trust| - User commands can support |:command-preview| to show results as you type - |:write| with "++p" flag creates parent directories. +- |:update| command writes new file buffers even when unmodified. Editor: - |prompt-buffer| supports multiline input/paste, undo/redo, and o/O normal diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index e2c918a0e3..e28f282dc4 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1772,7 +1772,9 @@ void ex_file(exarg_T *eap) /// ":update". void ex_update(exarg_T *eap) { - if (curbufIsChanged() || (curbuf->b_ffname != NULL && !os_path_exists(curbuf->b_ffname))) { + if (curbufIsChanged() + || (!bt_nofilename(curbuf) && curbuf->b_ffname != NULL + && !os_path_exists(curbuf->b_ffname))) { do_write(eap); } } diff --git a/test/functional/ex_cmds/write_spec.lua b/test/functional/ex_cmds/write_spec.lua index 15055175e7..668fc3ecf4 100644 --- a/test/functional/ex_cmds/write_spec.lua +++ b/test/functional/ex_cmds/write_spec.lua @@ -258,4 +258,15 @@ describe(':update', function() eq(1, eval('g:write_pre')) eq(1, eval('g:write_post')) end) + + it('does not write acwrite buffer when unchanged', function() + command('file remote://test') + command('setlocal buftype=acwrite') + command('let g:triggered = 0 | autocmd BufWriteCmd remote://* let g:triggered = 1') + command('update') + eq(0, eval('g:triggered')) + command('call setline(1, ["hello"])') + command('update') + eq(1, eval('g:triggered')) + end) end)