Yet another reminder that you have to think about file IO as a concurrency problem: https://thomask.sdf.org/blog/2019/11/09/take-care-editing-bash-scripts.html
To confuse matters further, the file saving strategy differs not only between text editors but within the same text editor depending on the context. There are basically two robust strategies for file saves:
1. Write new version to newly created foo.txt~ and atomically rename foo.txt~ to foo.txt.
2. Copy backup of foo.txt to foo.txt~, overwrite foo.txt in place with new version, delete foo.txt~.