2

I want to insert current date into buffer.

I did in my .vimrc - it works:

nnoremap <F5> "=strftime("%Y%m%d %H:%M:%S %Z")<CR>P

But how can I create a Vim command (e.g.: :insertdate) that does the same?

Because CLI vim via SSH doesn't handle Fn keys, as it seems. (Or at least in my setup.)

4
  • Nitpicking: you're not looking for a function but for a user command. I took the liberty to tag your question accordingly.
    – Friedrich
    Feb 2 at 12:08
  • Ah, thanks for that. Feb 2 at 12:13
  • As for the Fn key issue: that's probably off-topic here. And we would need a lot more information to solve it.
    – Friedrich
    Feb 2 at 12:39
  • Do you still have something open in your question? How can we help you further. Otherwise maybe could you accept one of the solution using the v button next to the arrow voting buttons. It allow the question to rest :-) Feb 3 at 15:50

3 Answers 3

3

You're looking to create a user command as documented in :help user-command.

User commands have to start with an upper-case letter to disambiguate them from the built-in commands. So :insertdate is off the table but e.g. :InsertDate would work.

I believe the following command would answer your question

:command! InsertDate normal! i<C-R>=strftime("%Y%m%d %H:%M:%S %Z")<CR>

This is a slight variation on your (and Vivian's) mapping. It enters insert mode and uses the expression register through <C-R>=.

Functions vs. User Commands

The question originally asked for a function to do this. This is also possible but is far less convenient. The function's body would look almost like the command above but it would have to be called using :call insertdate(). Nobody wants to type that.

3
  • 1
    Probably normal! unless you need normal?
    – D. Ben Knoble
    Feb 2 at 14:44
  • @D.BenKnoble no, just sloppiness. Thank you for pointing out.
    – Friedrich
    Feb 2 at 16:22
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Feb 6 at 5:39
2

The custom command must start with an upper case:

You can create the corresponding InsertDate command like:

command! InsertDate normal! "=strftime("%Y%m%d %H:%M:%S %Z")<CR>P
3
  • 1
    Damn your FTL answers... I was just typing it out :-D
    – Friedrich
    Feb 2 at 12:17
  • Sorry for that :-| I should have guessed ;-) Feb 2 at 12:22
  • 1
    No need to apologize... it's a game. Fastest wins :-) And mine also looks more clumsy :-)
    – Friedrich
    Feb 2 at 12:24
1

As an alternative to a user command, here are my insert-mode abbreviations for inserting a timestamp:

:iab <expr> dts strftime("%F %R")    " Date/time stamp
:iab <expr> dtg strftime("%e %b %Y") " Date style used in genealogy

(I'm a bit of a genealogy guy, but I've never used the second one in vim.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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