0

I am using Vim as my primary text editor on Fedora 34 workstation. I need to create and maintain a log file for one of my courses for the purpose of tracking time and work done. It should look something like this:

John Doe
Log for XYZ 1101, Fall 2021

***** IN:  Thu Aug 26 08:13:11 PM EDT 2021

Enter my log notes here

***** OUT: Thu Aug 26 09:22:13 PM EDT 2021

NOTE: two spaces after IN: and one space after OUT:


I would love to automate this process so that every time I open this document (if not possible, then on a given command) ***** IN: date gets printed and the cursor gets placed at the beginning of the second line below. Also, every time I close the document (or on a given command) ***** OUT: date (and a blank line after that) should be printed two lines below my last text entry. That will assure that there is always an empty line in between two given entries and in between my notes and entry dates.

Can anyone please help me do this or at least guide me to completing it? I have never done any scripting or messed with advanced vim settings.


EDIT: Using a comment from @muru and after looking up how to use autocmd, I was able to come up with the following.

autocmd VimEnter ~/test.log $pu=strftime('***** IN:  %c')
autocmd ExitPre  ~/test.log $pu=strftime('***** OUT: %c')

It works, but it is not perfect. The problems are:

  1. It does not leave me with an extra empty line in between text. I would love to be able to add 2 line breaks. How can I do that using Vims put command? Or is there any other way?

  2. The ExitPre command was the only on that somewhat worked for me. However, every time I do :w and :q (or :x) the OUT date gets written and the program does not exit. Instead, I have to save it again and the force an exit with :q!. I don't really consider this elegant option. I would want this to happen in the background so that I can exit only once, and then see the OUT date next time I open the document. Other commands like VimLeave (before exiting Vim, after writing the viminfo file) did not work at all.

Any help would be appreciated!

2

1 Answer 1

1

The current problems can be taped over:

autocmd VimEnter ~/test.log $pu=strftime(\"***** IN:  %c\n\n\n\")
autocmd ExitPre  ~/test.log $pu=strftime('***** OUT: %c') | w
  • \n is expanded in double quotes, but you need to escape the quotes when using =. Or you could use call append("$", [strftime("***** IN: %c"), "", ""]). Or simply add a | norm G2o instead.
  • |w will write the modifications you just made using $pu. The quit originally failed because $pu made unsaved modifications.
3
  • Works great if I do :w and then :q. However, if I do just :q or :q! , it will still save everything that I wrote. That is still a problem because I might just want to look at the document without recording an entry. Any advice?
    – Vladimir
    Commented Aug 27, 2021 at 14:50
  • 1
    @Vladimir you could u the IN timestamp, and then use cq to quit (I'm not sure why that works, but it does). Or you could write a small function that checks if the file's third-last starts with ***** IN, removes the last three lines if it is, or adds the ***** OUT if it isn't.
    – muru
    Commented Aug 28, 2021 at 3:53
  • Thank you so much!
    – Vladimir
    Commented Sep 1, 2021 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .