Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit a file in-place in vim

Tags:

vim

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?

like image 989
William Pursell Avatar asked Jun 15 '12 01:06

William Pursell


2 Answers

Check out the help for the backupcopy option and the notes on crontab in particular. The short answer is

:set backupcopy=yes
like image 75
evil otto Avatar answered Sep 23 '22 23:09

evil otto


Quick google shows this link - Editing a hard link to a file. The command that controls that is

set backupcopy=auto,breakhardlink
like image 32
Doncho Gunchev Avatar answered Sep 23 '22 23:09

Doncho Gunchev