VI / VIM: Insert Current Date / Time

In VI or VIM, you can insert the current date and time by using the following command:

  1. Press the Esc key to enter normal mode.
  2. Type : to enter command mode.
  3. Type r followed by the current date and time in the format you want to use. For example, to insert the current date and time in the format YYYY-MM-DD HH:MM:SS, use the following command:
:r!date +"%Y-%m-%d %T"
  1. Press Enter. The current date and time will be inserted into the file at the current cursor position.

Note: The ! character before the date command allows you to run a shell command from within VIM. The date command is used to display the current date and time, and the +"%Y-%m-%d %T" option is used to specify the desired format for the output.

Leave a Comment