Alaya
Alaya

Reputation: 3387

How could I judge whether the file has been changed since last saving in vim

I am trying to quantify my vim action by logging timestamp when saving(:w) a file.

Is it possible to know whether the file had really been edited since the last saving?

for example, I saved the file by :w, then, did nothing and just type :w to save it again, is it possible to judge that the file wasn't actually modified?

Upvotes: 9

Views: 3409

Answers (5)

Fillipos Christou
Fillipos Christou

Reputation: 95

press :file and if changes have been made [Modified] will be displayed after your file name.

Upvotes: 4

romainl
romainl

Reputation: 196526

You can check the value of the 'modified' option:

if &modified
    " do something if the buffer is modified
else
    " do something else if it is not
endif

Upvotes: 6

Adcaelo
Adcaelo

Reputation: 19

Before reading your answer, I thought it was about knowing if the file had been updated from another vim instance / editor before saving it!

If so, vim usally tell me when I want to save it :

WARNING : the file has been updated since vim read it !

Do you want to write it anyway (y/n)?

If it's only about knowing if you change anything since last time, as explained before, Vim display a [+] after your filename.

Upvotes: 0

A wild elephant
A wild elephant

Reputation: 177

Vim displays a '[+]' next to the file name when the file has been modified. However it displays it even if there is no diff between the file on disk and your buffer (for example if you add the letter 'a' then remove it it will consider that the file has been edited).

Upvotes: 9

mkrieger1
mkrieger1

Reputation: 23144

If you have set laststatus to 2 in Vim, it shows you a status line at the bottom of the window which has a [+] indicator next to the filename when the file has been modified since it was last written.

Relevant help pages: laststatus, statusline

Upvotes: 5

Related Questions