Store and run ex: commands in text file

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:

"zY@z
  • The result is:

:r /etc/hosts
127.0.0.1   localhost
127.0.1.1   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

Explanation:


"zY

Put current line to buffer with name 'z'


@z

Execute content of buffer 'z' as ex: command

Vim short form:


Y@"

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.

Examples:


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

:r!date --iso-8601=seconds
2014-06-25T19:52:28+0400

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

Last posts

  1. Selectively set 'execute' permission for directories.

    tags: POSIXfilesystem

  2. Copy directory tree with access rights but without files

    tags: POSIXfilesystem

  3. Force Linux user to change password

    tags: userpassword

  4. Redirect STDERR to STDOUT and then to file

    tags: shellbash