Store and run ex: commands in text file

Wed 25 June 2014 by Mikhail Basov

It is possible to store any ex: commands in text file and then execute them.
For example. I want to periodically insert current content of some file in other text file.

Do the following:

  • Put to file the following line

:r /etc/hosts
  • Set cursor to this line an execute in command mode:

  • The result is:

:r /etc/hosts   localhost   HOSTNAME

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters



Put current line to buffer with name 'z'


Execute content of buffer 'z' as ex: command

Vim short form:


This command use unnamed buffer register. 'Y' put current string to unnamed register, @" execute content of unnamed register as ex: command. This operate only in Vim and doesn't operate in POSIX vi.


:r!echo 'scale=2; ( 2 + 3 ) / 2' | bc

:r!date --iso-8601=seconds

:r!lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.4 LTS
Release:    12.04
Codename:   precise