Vim Tips Wiki
m (Save a buffer without changing the file's "last modified" attribute moved to Save buffer without changing Last Modified attribute: Page moved by JohnBot to improve title)
(TipImported + severe clean has been completed for tips 1 to 1001!)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1001
 
|id=1001
  +
|previous=999
|title=Save a buffer without changing the file's "last modified" attribute
 
  +
|next=1002
|created=September 23, 2005 2:42
+
|created=September 23, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=Craig Emery
 
|author=Craig Emery
 
|version=5.7
 
|version=5.7
 
|rating=1/5
 
|rating=1/5
 
}}
|text=
 
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.
+
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 <tt>+python</tt> you can define the following function and call it instead of using the write command.
   
  +
<pre>
 
function! WritePreserveMtime()
 
python &lt;&lt; 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
  +
</pre>
   
  +
See {{help|python}} for information on calling Python from inside Vim.
This is often because I'm simply changing a comment in a source file and I don't need my build system to re-compile the file.
 
   
 
Since I
  +
<pre>
 
:map &lt;F3&gt; :w&lt;CR&gt;&lt;C-G&gt;
  +
</pre>
   
 
I also
  +
<pre>
 
:map &lt;S-F3&gt; :call WritePreserveMtime()&lt;CR&gt;&lt;C-G&gt;
  +
</pre>
   
 
==Comments==
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 &lt;&lt; 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 [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:python}} :help python] for more help on calling Python from inside vim.
 
 
 
 
Since I
 
 
 
 
:map &lt;F3&gt; :w&lt;CR&gt;&lt;C-G&gt;
 
 
 
 
I also
 
 
 
 
:map &lt;S-F3&gt; :call WritePreserveMtime()&lt;CR&gt;&lt;C-G&gt;
 
}}
 
   
  +
----
== Comments ==
 
<!-- parsed by vimtips.py in 0.661038 seconds-->
 

Revision as of 08:05, 10 December 2007

Tip 1001 Printable Monobook Previous Next

created September 23, 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>

Comments