Fandom

Vim Tips Wiki

Save buffer without changing Last Modified attribute

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 1001 Printable Monobook Previous Next

created 2005 · complexity basic · author Craig Emery · version 5.7


There are times when I change something in a file and I've no need for the file's "last modified" time to be changed. For example, I might be updating a comment in a source file, and I don't need my build system to re-compile the file.

If your build of Vim has +python you can define the following function and call it instead of using the write command.

function! WritePreserveMtime()
python << EEOOFF
import vim
import os.path
import os
fpath = vim.current.buffer.name
atime = os.path.getatime(fpath)
mtime = os.path.getmtime(fpath)
vim.command("w")
os.utime(fpath, (atime, mtime))
EEOOFF
endfunction

See :help python for information on calling Python from inside Vim.

Since I

:map <F3> :w<CR><C-G>

I also

:map <S-F3> :call WritePreserveMtime()<CR><C-G>

CommentsEdit

Also on Fandom

Random Wiki