1

I would like help with my vim script. thus far I am able to check for the file name, however, I have been unsuccessful in learning how to give normal mode commands via vim script. here is what I've got:

autocmd VimEnter * if @% == 'diary.md' | :r! | endif

I am trying to give the command :r! to add the date/time on a new line when I open this file.

I appreciate any and all wisdom, thanks~!

Rokiora
  • 21
  • 5
  • 1
    Well, `:r!` expects an external command but we don't know what systems you would use that on so it's hard to give you a definitive answer. On unix-derivatives, you would probably use `$ man date`. Or you could use the built-in `:help :put`, `:help localtime()`, and `:help strftime()` instead for better portability. – romainl Jan 17 '22 at 08:34

2 Answers2

2

From vi.stackexchange.com, a seemingly easy answer:

 autocmd VimEnter dairy.md | 0put=strftime('%c') 

You just have to use the filename, the zero indicates the very beginning of the file. If you want to modificate the timestamp just read the strftime help for that.

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • thank you so much~! may if I may bother you a bit more, how do you recommend I go about learning more vimscript? at the moment I just googl search like mad, is there a better method? thanks in advance~! – Rokiora Jan 17 '22 at 21:38
  • Actually I recommend learning neovim with lua settings, because luajit is almost faster than vim9, which is still a project for better vimscript. Neovim has in my view a more open community, specially when it comes to the developing model. – SergioAraujo Jan 19 '22 at 04:04
1

huge thanks to @romaini and @SergioAraujo~!

@romaini - your comment helped me learn more about external commands and using the built in :help

@SergioAraujo - your answer was right on the money~! the only issue was that I received the following error with your code:

E116: Invalid Arguments for function strftime(

It was a quick fix though by switching from ("%c") to ('%c') :

autocmd VimEnter diary.md | $put=strftime('%c')

I am unsure why it only works with single quotes

Additionally, you will note I used $put instead of 0put, this is because I wanted to add the timestamp at the end of the file as a way to start a new entry of my diary on this document. Once again thank you for all your help~!

Rokiora
  • 21
  • 5