Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to insert timestamp in Vim?

Tags:

vim

vi

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.

What is the best way to get this functionality in Vim?

(I am using Vim 6.1 on a Linux server via SSH. In the current situation a number of us share a login so I don't want to create abbreviations in the home directory if there is another built-in way to get a timestamp.)

like image 537
Edward Tanguay Avatar asked Sep 11 '08 08:09

Edward Tanguay


People also ask

What command will insert the current date into the file you are editing in vim?

In vim you can execute comands with "!". You can combine that with "r" to insert the output into your current buffer. will insert the date into a file. Which out puts the name of the file and puts F after it.

What command will insert the current date below the line you are editing?

date – Uses your system's date command and reads the results into the line below. This is dependent on your underlying system having a date command. On Windows, use date \t . :put =strftime('%B %e') – Uses Vim's strftime function to insert a date time using standard strftime formatting.


1 Answers

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().

like image 152
Swaroop C H Avatar answered Sep 22 '22 22:09

Swaroop C H