Best way to insert timestamp in Vim?

前端 未结 10 1823
轮回少年
轮回少年 2020-11-30 18:25

EditPad Lite has a nice feature (CTRL-E, CTRL-I) which inserts a time stamp e.g. \"2008-09-11 10:34:53\" into your code.

相关标签:
10条回答
  • 2020-11-30 18:35

    To make it work cross-platform, just put the following in your vimrc:

    nmap <F3> i<C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR><Esc>
    imap <F3> <C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR>
    

    Now you can just press F3 any time inside Vi/Vim and you'll get a timestamp like 2016-01-25 Mo 12:44 inserted at the cursor.

    For a complete description of the available parameters check the documentation of the C function strftime().

    0 讨论(0)
  • 2020-11-30 18:35

    Why is everybody using :r!? Find a blank line and type !!date from command-mode. Save a keystroke!

    [n.b. This will pipe the current line into stdin, and then replace the line with the command output; hence the "find a blank line" part.]

    0 讨论(0)
  • 2020-11-30 18:35

    For a unix timestamp:

    :r! date +\%s
    

    You can also map this command to a key (for example F12) in VIM if you use it a lot:

    Put this in your .vimrc:

    
    map  <F12> :r! date +\%s<cr>
    
    0 讨论(0)
  • 2020-11-30 18:43

    Another quick way not included by previous answers: type-

    !!date

    0 讨论(0)
  • 2020-11-30 18:45

    Unix,use:

    !!date
    

    Windows, use:

    !!date /t
    

    More details:see Insert_current_date_or_time

    0 讨论(0)
  • 2020-11-30 18:50

    As an extension to @Swaroop C H's answer,

    ^R=strftime("%FT%T%z")
    

    is a more compact form that will also print the time zone (actually the difference from UTC, in an ISO-8601-compliant form).

    If you prefer to use an external tool for some reason,

    :r !date --rfc-3339=s
    

    will give you a full RFC-3339 compliant timestamp; use ns instead of s for Spock-like precision, and pipe through tr ' ' T to use a capital T instead of a space between date and time.

    Also you might find it useful to know that

    :source somefile.vim
    

    will read in commands from somefile.vim: this way you could set up a custom set of mappings, etc., and then load it when you're using vim on that account.

    0 讨论(0)
提交回复
热议问题