Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim - insert timestamp in the current line after some words?

Tags:

vim

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.

like image 607
fizis Avatar asked Jan 27 '26 04:01

fizis


2 Answers

All you have to do is mapping F3:

inoremap <special> <F3> <c-r>=strftime('%c')<CR>

With this way whenever you press F3 button it will insert timestamp at current position during insert-mode.


Explanation:

[i][noremap] [<special>] [<F3>] [<c-r>=strftime('%c')<CR>]
 |   |          |          |      |
 |   |          |          |      |
 |   |          |          |      |---> Whenever you press <F3> button it 
 |   |          |          |      |---> will execute this part
 |   |          |          |      |---> it is like pressing ctrl-r 
 |   |          |          |      |---> then pressing = and typing
 |   |          |          |      |---> strftime('%c') and then 
 |   |          |          |      |---> typing enter (<cr>) for execution
 |   |          |          | 
 |   |          |          |---> The key that you want to map
 |   |          |
 |   |          |----> This argument is for avoiding side effect of 'cpoption'
 |   |
 |   |-----> The  mapping (noremap) is not recursive
 |   |
 |   |-----> It means that it doesn't care about other mappings 
 |   
 |---------> (i) for insert-mode (n) for normal-mode (c) for command-mode ...

For more see :help map.txt

like image 116
Meninx - メネンックス Avatar answered Jan 30 '26 08:01

Meninx - メネンックス


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
like image 22
SergioAraujo Avatar answered Jan 30 '26 07:01

SergioAraujo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!