'vim - insert timestamp in the current line after some words?
I want insert the timestamp in the current line without ruin the content in that line. I've read this post. Basically it suggests:1). !r date which insert on the next line; 2). !!date which clear current line and insert the timestamp.
Solution 1:[1]
iab idate <c-r>=strftime("%c")<cr>
No need to use any shortcut, just type "idate" or any other word of your choice (iab stands for insert mode abbreviation)
If you have ultisnips plugin you can create a snippet like this:
snippet date "use tab to insert date" w
`date +%Y-%m-%d\ %H:%M` ${0}
endsnippet
Solution 2:[2]
As SergioAraujo suggested, you can use ultisnips together with vim-snippets. Ultisnips is the snippet engine and vim-snippets provides the actual snippets. You can also install the auto-complete deoplete to complete your snippet.
Back to your question, vim-snippets provides several time or date related snippets, for example date, time, datetime, diso etc.. You can find them here. After input the snippet keyword, you can press Tab to expand the snippet to its full content.
Solution 3:[3]
I have a set of mapped keys for inserting timestamps at my cursor's position.
"--- datetime stamps
inoremap dts <c-r>=strftime("%Y%m%d_%H%M%S")<CR>
inoremap dtz <c-r>=strftime("%F %T%z")<CR>
"--- date stamps
inoremap ymd <c-r>=strftime("%Y%m%d")<CR>
inoremap ymd- <c-r>=strftime("%Y-%m-%d")<CR>
inoremap dny <c-r>=strftime("%d-%b-%Y")<CR>
"--- time stamps
inoremap tz <c-r>=strftime("%T%z")<CR>
inoremap ts <c-r>=strftime("%H%M%S")<CR>
... inoremap used, as well as nnoremap for normal mode.
Your mileage may vary, your formats most definitely will vary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Telemachus |
| Solution 2 | |
| Solution 3 | Cometsong |
