Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The tutorial mentions that the Ex-mode is for batch processing. Since it is a nuisance, rather than a tool, for me, I would like to see some practical examples. Who uses it? Why?

What is the EX-mode for batch processing for?

share|improve this question

migrated from Aug 13 '09 at 19:46

This question came from our site for professional and enthusiast programmers.

@Masi: link your accounts so migrated posts will be associated with your SU account after migration. – Greg Hewgill Aug 13 '09 at 20:32
@Greg: Thank You. – Masi Aug 13 '09 at 22:35

3 Answers 3

up vote 1 down vote accepted

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/

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 
let @t='<table>'.repeat('<tr>'.repeat('<td>_</td>',20).'</tr>',20).'</table>'
%s/_/\=system('echo $RANDOM')/g


ex -s table.html < table.vim

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

See also:

share|improve this answer

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
share|improve this answer
A full —and more terse— implementation would be for file in *.html; do vim -es -c '%s/home\.html/index.html/g' -c wq "$file"; done. – intuited Oct 26 '10 at 6:45

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.

share|improve this answer
This seems quite far-fetched, but it's for sure reasonable and clever! +1 – Tarrasch Jul 31 '12 at 14:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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