How can I read a file into the current buffer and do some template substitution on it automatically?


I'm working on a simple bash-driven logging / journaling capability. (on github, fyi)

I have a file that contains a templated todo item called daily.tmpl as follows:

- [ ] Check this weeks todo: 2020-$WW.md

I would like to be able to read that into my daily todo, and substitute $WW with the week.

I can capture the week number using date +%W. Or in vim: :let weeknumber=date +%W (with appropriate escaping omitted for clarity).

I can read the templated file to the top using :read daily.tmpl

So, I have created a bash alias that can open a daily todo item with a file name by date as:

alias todo=vim -c 'read daily.tmpl' `date`

Option 1

What command can I execute to substitute, after read, $WW for the local vim variable weeknumber.

Option 2

Is there another, better way to do this?

1 Answer 1


First, let weeknumber=date +%W won't work at all. You want something like let weeknumber=strftime("%W").

Second, the command for the substitution would be


But simpler is


Is there a better way

You could put the vimscript into a file (e.g., todo.vim) and run vim -S todo.vim filename.... Taking this further, you could use a here-document if your shell supports it:

vim filename -S <(cat <<DOG

See :help -S, :help :substitute, :help range, and :help s/\=

  • 1
    Instead of system() it's even better to use strftime('%W'). Also "a dollar sign" need not be escaped, as it is followed by a letter anyway (see :h /$)
    – Matt
    Apr 3, 2020 at 17:22
  • Ah, very clean. I knew it would be coming from the right source. Apr 3, 2020 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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