1

I have a custom command for stamping time in my log files. However, I would like that format to match the one of bash command date.

For example, I use this command:

:command InLog pu=strftime(\"\n***** IN:  %c\n\n\n\") | startinsert

which prints:

***** IN: Thu Sep 16 01:51:47 2021

However, I would want to have the same output as the date command in bash. Its output is:

Thu Sep 16 01:52:48 EDT 2021

Notice the EDT!

I would appreciate any help. :) Thank you in advance!

4
  • 1
    Why not use the shell command itself with system('date'). So your command would be something like :command InLog pu=printf("\n***** IN: %s\n\n\n", system('date')) | startinsert. (Unless you want to share this and need it to be portable...)
    – B Layer
    Sep 16 '21 at 6:16
  • Part of the reason is because I am new to Vim so I did not know to do it the way you suggested. I like that method; however, there is an error. It says: Invalid arguments for function printf(
    – Vladimir
    Sep 16 '21 at 6:20
  • Hmmm. Put backslashes in front of the " like in your original command...?
    – B Layer
    Sep 16 '21 at 6:23
  • 4
    how about: :echo strftime('%a %b %d %T %Z %Y') Sep 16 '21 at 6:27
2

While Christian Brabandt already gave the correct answer in a comment:

:echo strftime('%a %b %d %T %Z %Y')

I would like to add a few words.

The most important part from the help documentation:

The accepted {format} depends on your system, thus this is not portable! See the manual page of the C function strftime() for the format.

So the actual valid format depends on your operating system, as Vim just uses the strftime available in the C library. Also most format specifiers should be portable, there might be differences.

The format specifier %c you used is described in the linux man page as:

The preferred date and time representation for the current locale. ...

As that it doesn't need a timezone.

See :help strftime() (and your local C library documentation).


Windows strftime

Linux man page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.