3 Answers
3

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:

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.