Normally, you don't actually edit a file in vim. If you run vim foo
, edit, write and quit, vim unlinks foo
and creates a new file and a new link named foo
. But, if the file has more than one link, then vim
actually modifies the file. For example:
$ ls -i foo
19428852 foo
$ vi foo
$ ls -i foo
19428857 foo
$ ln foo bar
$ vi bar
$ ls -i bar
19428857 bar
I've been looking around for a way to actually edit a file, and it seems there are two choices. Use ed
, or use vim
but ensure that there are at least two links to the file before I edit. I suppose the question is academic, as it doesn't really matter that vim creates a new file if there is only one link, and vim seems to do the right thing if links are created while the editing is underway, but I'm curious:
is it possible to make vim always edit the file, and not create a new one?
Check out the help for the backupcopy
option and the notes on crontab
in particular. The short answer is
:set backupcopy=yes
Quick google shows this link - Editing a hard link to a file. The command that controls that is
set backupcopy=auto,breakhardlink
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With