Saluton J.A.J. :)

On Thu 14 May 2009 09:34 +0200, J.A.J. Pater <[email protected]> dixit:
> But I'd like vim to write time to a log file.
> For instance:  write the (system) time when a file is opened and
> again write the time to the same log file when a file is closed.

You can do it with autocommands. It is not trivial but it is not
complex, either. Try ":help autocmd" and ":help autocmd-events". Check
for the event you need (when the file was written, when the file was
opened, when the buffer was wiped, etc.). Depending on your needs, the
particular event to use will be different.

After that you will have to write what to do when the event happens. You
have to use the "autocmd" command for that (you can put it in your
.vimrc file if you want the autocmd to be run for all kind of files, for
example). As for dumping the time, maybe the easier way would do using
":!date >> logfilename".

Using vimscript you can do much more ellaborate things, like creating a
scratch buffer, dump the time to it (modifying the exact string in the
process, or maybe computing the duration instead of just dumping
timestamps), save to different names, etc.

The Vim help contains all the information you need for that (see ":help
eval", for example) and although it will take you some time, I think it
is worth the effort if you plan to use Vim frequently. It's not very
difficult to write small snippets of code to perform some automatic
tasks (for example, in this message the attribution line and the
salutation were written by Vim automatically, not by me).

While most of the time you won't need to write a single line of
vimscript to achieve what you want, in cases like this one you're asking
about now, it is very useful. You can do it by hand, remembering to run
":!date >> logfilename" when you start editing and when you close the
file, but you can have Vim do this for you.

> Or: (even better) just write the span of time which has elapsed since
> the file was opened on closing the file.

You will probably need BufHidden to know when the file was closed, but
that won't work when using ":q" when exiting Vim, so you may need to
catch BufDelete too. I mean, depending on how you start editing the file
and how you stop editing it (closing Vim, closing the file, saving the
file, etc.) you may need to do the actions in more than one event.

Probably somebody in the list with more experience in Vimcript can write
the autocommands for you. Myself, I would have to carry some tests and
check the documentation, so I can't afford the time, sorry.

-- 
Raúl "DervishD" Núñez de Arenas Coronado
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to