Alex
Alex

Reputation: 667

Is there a way to have "virtual" editing in Vim?

It is difficult to describe with 1 or 2 words so here I will give a use case:

  1. UserA creates a text file which acts as a fillable form/checklist
  2. UserA saves the file to a shared repository
  3. UserB and UserC open the file and fill the form/checklist as they do their work
  4. Changes to fields (ideally) or to the whole file are ignored if file saved by accident

Note: UserB and UserC may need to make structural changes to the text file also sometimes, so forbidding them write/modify access at file-system/file-share levels is not an option. It has to be a conscious decision when structural editing is needed (e.g. a user opens the file with a special flag to vim or presses an "unlock" key sequence after opening the file).

Upvotes: 1

Views: 590

Answers (1)

David Pope
David Pope

Reputation: 6637

The workflow you describe suggests that UserB and UserC don't actually need to save the file (unless they're doing structural edits), they just usually need it open so they can mark quick line items done as they complete them. (If they actually do need their edits saved, you'd need to supply a lot more detail in your use case, and then I'd agree with the commenters anyway that you need a more robust solution.)

The easiest way to do this is to mark the file as readonly in the filesystem. On Windows, you can do this with attrib +r filename.txt. I forget whether there's an equivalent attribute in Unix-style systems, or if you instead have to actually remove write permissions completely.

When UserB or UserC need to save their changes for structural edits, they do :w! instead of the usual :w to save the file. This forces vim to ignore the readonly attribute and save the file anyway.

Upvotes: 1

Related Questions