It is completely possible to build on the linked duplicate for a specific list of files.
We first extract the old code into a function that will operate on a given filename:
function! ReallyWrite(f) abort
execute 'buffer' f
let lastline = line('$')
let bufcontents = getline(1, lastline)
call setline(1, bufcontents)
if line('$') > lastline
execute lastline+1.',$:d _'
Then we write a command to do it for all the files we want:
command -nargs=+ -complete=file ReallyWrite for f in [<f-args>] | call ReallyWrite(f) | endfor
:ReallyWrite file1 file2
With some simple modifications,
ReallyWrite could return to the old buffer after it is finished working. (I think you'd just have to do
buffer # at the end of the function.)
This answer is a two-parter:
- "Tricking" vim into not caring about the fact that files have changed
- Using git properly to avoid this in the first place
:help :wall tells us that
Vim will warn you if you try to overwrite a file that has been changed
elsewhere. See |timestamp|.
:help timestamp gives
If you do not want to be asked or automatically reload the file, you can use
Now, the problem with
buftype=nofile is that the file
will not be written (
So, the linked QA provides one answer (save and restore). My solution would be to set all buffers to
nofile and then use
:w filename (which should still work, according to the docs):
argdo set buftype=nofile | execute 'write' bufname('%')
except that, for some reason, the docs don't seem to be correct: the above command errors for each argument, even though
:write filename is supposed to work.
:write othername works, but you cannot write to the buffer name.
So I would use the linked answer.
I'm not sure how on-topic this part is, but it's an answer to the question, if a bit of a frame challenge.
You want to get (some of) the files in one branch (
to_update) to have identical contents to the corresponding files on another branch (
- Checkout the
- Choose a strategy:
- Merge the branches:
git merge stable. You don't have control over which files change, and they won't be identical post-merge, but you do get the contents from the
stable branch (integrated with
- Checkout the files on another branch:
git checkout stable [files...]. This makes
files exactly as they are in
stable, and lets you pick which files. No vim reloading necessary.
Caveat: if you have already edited the files, then you switch branches, you have a different problem. You wouldn't be allowed to switch branches if there were conflicts, so the vim method should work fine. If there were conflicts, do
git stash, the use the second strategy for git, the
git stash apply (followed by
git stash drop if you're satisfied).
If you wish to edit the files that changed, you can do some parsing to get a list of changed files. I do this with my