Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
|id=1001
 
|id=1001
 
|previous=999
 
|previous=999
|next=1002
+
|next=1003
|created=September 23, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Craig Emery
 
|author=Craig Emery
Line 14: Line 14:
 
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.
 
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.
+
If your build of Vim has <code>+python</code> you can define the following function and call it instead of using the write command.
   
 
<pre>
 
<pre>
 
function! WritePreserveMtime()
 
function! WritePreserveMtime()
python &lt;&lt; EEOOFF
+
python << EEOOFF
 
import vim
 
import vim
 
import os.path
 
import os.path
Line 35: Line 35:
 
Since I
 
Since I
 
<pre>
 
<pre>
:map &lt;F3&gt; :w&lt;CR&gt;&lt;C-G&gt;
+
:map <F3> :w<CR><C-G>
 
</pre>
 
</pre>
   
 
I also
 
I also
 
<pre>
 
<pre>
:map &lt;S-F3&gt; :call WritePreserveMtime()&lt;CR&gt;&lt;C-G&gt;
+
:map &lt;S-F3> :call WritePreserveMtime()<CR><C-G>
 
</pre>
 
</pre>
   
 
==Comments==
 
==Comments==
 
----
 

Latest revision as of 06:03, 13 July 2012

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>

Comments[]