Reputation: 2470
I'm trying to keep track of my hours for work, and I have to keep very specific details of which files I was editing and when. In my .vimrc script, I have:
:silent !clock_in_script.sh %:p
This clock_in_script.sh takes the location of the file as an argument and just writes the name of the file and time to a log.
Is there a way to similarly invoke a script when I exit vim? If not, is there a way I can write the name of the file I'm editing and the time to a logfile from vim?
Thank you in advance!
Upvotes: 10
Views: 5422
Reputation: 2470
Thank you to those of you who suggested autocmd events. The events that I have found to be most appropriate are BufWinEnter and BufWinLeave. Here's exactly what I put in my vim:
:autocmd BufWinEnter * !clock_edit %:p
:autocmd BufWinLeave * !clock_close %:p
clock_edit bash script:
#!/bin/bash
echo "`date`:: opened file $1" >> ~/.clock_log
clock_close:
#!/bin/bash
echo "`date`:: closed file $1" >> ~/.
For example, when I open config.txt
, vim will invoke clock_edit /node/proj/config.txt
.
If I open several files as tabs, each one I open will invoke clock_open and each one I close will invoke clock_close, with the appropriate filename and path passed as arguments. Even better, it doesn't repeat the process when I switch tabs or window focus, just when I open and close the files.
Thank you for all your help!
edit: autocmd is a useful way of binding vim commands to events ( such as saving a file ) in vim. For more info, read about autocmd here.
Upvotes: 10
Reputation: 195179
there is an event VimLeave
(:h VimLeave) is for the requirement you described.
However, this may not work for your needs. Because you may open many files in one vim instance. I use vim always in this way. In this case, you can only save the status of current buffer/file.
You can :h event
to check out the events vim's autocommand supports. Also consider to write a small function to loop all buffers and call your command. or modify your shell script to accept more than one filename as parameter, so that you can call the script only once.
Upvotes: 5
Reputation: 207670
Not sure exactly what you want to do, but you could maybe concoct something with autocommands, and the BufRead and BufWrite parameters. See here and also here.
Upvotes: 3