Best way to insert timestamp in Vim?

前端 未结 10 1824
轮回少年
轮回少年 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:50

    :r! date

    You can then add format to the date command (man date) if you want the exact same format and add this as a vim alias as well

    :r! date +"\%Y-\%m-\%d \%H:\%M:\%S"

    That produces the format you showed in your example (date in the shell does not use \%, but just %, vim replaces % by the name of the current file, so you need to escape it).

    You can add a map in your .vimrc for it to put the command automatically, for instance, each time you press F3:

    :map <F3> :r! date +"\%Y-\%m-\%d \%H:\%M:\%S"<cr>
    

    (Edited the from above :) ) (Edit: change text part to code, so that

    <F3> 
    

    can be displayed)

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

    I wanted a custom command :Date (not a key mapping) to insert the date at the current cursor position.

    Unfortunately straightforward commands like r!date result in a new line. So finally I came up with the following:

    command Date execute "normal i<C-R>=strftime('%F %T')<CR><ESC>"
    

    which adds the date/time string at the cursor position without adding any new line (change to normal a add after the cursor position).

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

    From the Vim Wikia.

    I use this instead of having to move my hand to hit an F key:

    :iab <expr> tds strftime("%F %b %T")
    

    Now in Insert mode it just type tds and as soon as I hit the space bar or return, I get the date and keep typing.

    I put the %b in there, because I like seeing the month name. The %F gives me something to sort by date. I might change that to %Y%m%d so there are no characters between the units.

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

    http://kenno.wordpress.com/2006/08/03/vim-tip-insert-time-stamp/

    Tried it out, it works on my mac:

    :r! date
    

    produces:

    Thu Sep 11 10:47:30 CEST 2008
    

    This:

    :r! date "+\%Y-\%m-\%d \%H:\%M:\%S"
    

    produces:

    2008-09-11 10:50:56
    
    0 讨论(0)
提交回复
热议问题