user137369
user137369

Reputation: 5666

Edit save history in vim

Let’s say I’m editing a file in vim, and I save (1). I continue editing, and I save again (2). Is it possible to “delete” this last save — meaning that vim would leave the file in its current state, but thinking the last save was 1?

To clarify, I open a file and write

one
two

I save it. Then I continue editing it, so it’s now

one
two
three
four

Now I need to save it in this state, and right after that do something (this is what I want to know) that will make vim still show me

one
two
three
four

but that if I close the file (or revert it with :e!, it’ll be at the state of

one
two

I want to do this because I use processing, and to use it with an external editor it relies on an external program that acts on the file, which means it needs to be saved, so the program can act upon the desired state of the file. Sometimes, however, I have a saved file and want to make a few changes just to test the result. However, if I don’t like the result, I’m “stuck” with this new saved version (unless I remember exactly how many times I would need to undo to get the previous saved state), and I’m looking for a way to chain this to the command that saves the file and runs the program against it.

Upvotes: 0

Views: 435

Answers (4)

echristopherson
echristopherson

Reputation: 7074

Here's a way to do it that builds on minitech's answer. Note that I haven't tested this extensively.

  1. Make your original edit and then save.
  2. Make your next edit and then save.
  3. Use :ea 1f to revert to the buffer's state at last save (see :h :earlier).
  4. Save again.
  5. Use :lat 1f to go back to the buffer as it looked after step 2 (see :h :later).
  6. Use :set nomodified so Vim doesn't think the buffer is modified and thus needs to be saved again.

The file on disk will still look like it did after step 1, because of the actions you took in step 3 and 4.

But I would also advise you to use version control instead. The fugitive plugin is a very nice way to use Git in Vim that doesn't get in your way.

Upvotes: 1

d11wtq
d11wtq

Reputation: 35298

It sounds like you just want to:

  1. Undo a change
  2. Save the file
  3. Redo the change

    :undo | w | redo<CR>

Upvotes: 0

FDinoff
FDinoff

Reputation: 31419

This was taken from your original edit.

I have a vim action that needs the file to be saved before acting on it, but I still want to keep the experimentation possibility open (so if I don’t like the changes, I can just do :e! or close the file. Is it possible?

What you are looking for is called version control. Version control allows you to make changes to files, save and undo those changes. Take a look at git, or svn.

Heres an example of what you can do with git.

Lets say you have a file with contents

one
two

Then you can check in your file into the version control software. (With git you do git commit)

Then later on you modify the file to be

one
two
three
four

At this point you can do a git diff and see all the changes you did to the file since your last commit.

If you like the changes you can commit them. If you don't like the changes you can do a git checkout <file> and the file is reverted to its old state of

one
two

Upvotes: 2

Ry-
Ry-

Reputation: 224857

Your two descriptions seem to conflict with each other, so I’ll answer the second one :)

To mark the current file as unmodified, it’s:

:set modified!

Upvotes: 0

Related Questions