2

I want to have a simple alias to take quick notes. Each note starts with the current date and time (ISO 8601). The alias should:

  • Start vim
  • Add the current date and time
  • Position the cursor in the next line
  • Switch to Insert mode

I tried to use Ex mode. So far I was able to do the following:

:i
<empty line>
<empty line>
.
:1!date --iso-8601=seconds
:2

I saved it in a file so I can source it when I run Vim using the -S option. Together with my new alias alias log="vim .notes -S .notes-source" it works quite well.

Anyway, I'd like to improve it a bit. After the last Ex command 2, can I tell Vim to automatically go to Insert mode?

Out of curiosity, can I put multiple Ex commands in one or more -c/--cmd options? Something like this (yep, it does not work):

vim -c "i\n\n." -c "1!date --iso-8601=seconds" -c "2"

The main problem I see is that the i Ex command requires new lines, and I'm not sure how to add them.

2

You can run following command at your command prompt:

vim -c "normal i`date --iso-8601=seconds`" -c "normal o" -c startinsert

Here is how it works: We are using three -c commands here:

  1. First command runs normal i followed by current actual date obtained using back tick shell operator. This basically inserts current date when vim is launched. Naturally the date gets added to first line, as you want.
  2. Second command adds one blank line.
  3. Third command switches vim to insert mode upon launch.

If you need more blank lines, you can pass a count to second -c command, something like below:

vim -c "normal i`date --iso-8601=seconds`" -c "normal 5o" -c startinsert

This command will insert 5 blank lines.

HTH.

  • Thanks! It might not have been clear from my question, but I want to use it several times on the same file. Your solution works, but after the first time I use it, the new date is added to the first line, together with the old one. I fixed using this oneliner vim -c "normal O`date --iso-8601=seconds`" -c "normal o" -c startinsert – vrde Aug 31 '18 at 14:27
  • Another small note, I want to avoid using backticks (I use fish shell, and it does not understand it, yes I can use other subshell commands, but I'd like to be agnostic) I use vim -c "normal O" -c ".!date --iso-8601=seconds" -c "normal o" -c "startinsert" <filename> – vrde Aug 31 '18 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.