What is the EX-mode for batch processing for?

Ex-Mode is mostly for performing the same action on a number of files.

Say you have 25 .html files all with:

<a href="/home.html"> ...

Instead of opening each one of those, you could use Ex-mode to change it all to index.html:

vim -E -s bob.html <<-EOF
   :%substitute/home.html/index.html/
   :update
   :quit
EOF

Vim in Ex mode (also known as ex) is useful when:

  • You're in need of editing (multiple) files non-interactively (as part of the script).
  • Your connection is very slow or screen is not updated after your actions.
  • Mappings and abbreviations are disabled.
  • Common keys such as Escape or Control doesn't work properly.

Editing files non-interactively is the most common usage and people using it in similar way as sed and awk, however they're are more stream oriented - they only read the file forward from beginning to end while vim is buffer oriented - you can move forward and backward in the file as you like. Secondly vim's regular expressions are more powerful than awk's and sed's expressions (they're not designed to work with multiple lines) - for example vim can match over several lines and supports zero matches.

Ex is also an editor (direct predecessor of vi) and in Vim - Ex Mode emulates ex (they still run the same code), so it is possible to get to the command mode of ex from within vi and vice-versa. There is Ex mode (vim -e) and improved Ex mode which allows for more advanced commands than the vi compatible Ex-mode (vim -E). See: What is the difference between Ex mode and improved Ex mode?

Ex is the root of a family of editors: edit, ex and vi. Ex is a super‐ set of ed, with the most notable extension being a display editing facility.ex(1)


Example 1

Here is simple example of changing 127 to 128 of your hosts file and print the output:

ex -s +%s/127/128/g +%p +q! /etc/hosts

is equivalent to:

sed s/127/128/g /etc/hosts

For more advanced solution, you may have file with vim commands and use it by a more classic approach to I/O redirection:

echo :%s/127/128/g > cmds.vim
echo :%print >> cmds.vim
echo :%quit! >> cmds.vim
ex -s /etc/hosts < cmds.vim # The same as: vim -s cmds.vim /etc/hosts

Example 2

You can also use vim plugins to perform some tasks.

For example the following one-liner would convert your source code file into html using one of the standard plugins:

ex -s '+let g:html_no_progress=1' '+syntax on' '+set ft=c' '+runtime syntax/2html.vim' -cwqa my_code.c

It'll generate html file (with syntax highlighting) from your code (written in any supported language) which is ready for printing or for publishing on your website.


Example 3

Or some real live example from the RPM specification:

vim -E -s Makefile <<-EOF
   :%substitute/CFLAGS = -g$/CFLAGS =-fPIC -DPIC -g/
   :%substitute/CFLAGS =$/CFLAGS =-fPIC -DPIC/
   :%substitute/ADAFLAGS =$/ADAFLAGS =-fPIC -DPIC/
   :update
   :quit
EOF

Example 4

The following script will create a new html file by downloading html of Example site and replacing its body by auto-generated 20x20 table with random numbers in it:

" table.vim
%!curl -s example.com 
let @t='<table>'.repeat('<tr>'.repeat('<td>_</td>',20).'</tr>',20).'</table>'
/<body
norm!vitd"tP
%s/_/\=system('echo $RANDOM')/g
wq

Usage:

ex -s table.html < table.vim

This will work on *nix like systems with curl installed. Add -V to see the script in action.

More examples:

  • How to edit files non-interactively (e.g. in pipeline)? at Vim SE
  • How can I replace a string with another string in a variable, a stream, a file, or in all the files in a directory?

See also:

  • Learning the vi Editor/Vim/Modes: Ex-mode
  • Does Ex mode have any practical use? at Vim SE

As of the recent 7.3 which as persistent undo support, ex mode and other forms of vim batch processing is superior to other non-vim methods, since it will not clobber the undo history.

Adding: 'persistent undo' if enabled, keeps all changes to a file (up to a limit) in the undo history, across vim editing sessions. If the file is edited by an external program, vim will reset the undo history when detecting by checksum that it has changed. vim -E will allow you to batch-edit a file and the edit will be in the undo history.

Tags:

Vim

Batch