19

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?

migrated from stackoverflow.com 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
4

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:


See also:

22

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
  • 6
    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
5

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.

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

This does not answer your question about batch processing, but it is another way to explain what Ex-mode is. You know how you press : to go into command-line mode and enter commands? Well Ex-mode also lets you enter the same commands, except Vim does not leave Ex mode after you enter a command, it stays in Ex mode and you can enter another command. So it is sort of like a REPL (read, evaluate, print, loop), but without the print (so just a REL...). I will get back to why you might want that kind of REL later. That is why it is called Ex mode, it lets you enter Ex commands, which is the same thing command-line mode does, it lets you enter an Ex command. Another way of explaining it, is that it lets you enter and immediately executes Vimscript/VimL code. Vimscript/VimL is a language you can use to write Vim plugins in. It is also the language you use to write your config (proably .vimrc) file in. It is also the language you are using when you enter Ex commands from command-line mode or Ex-mode. So from that perspective, Ex-mode can be seen as a VimL REL. Why would you want a VimL REL? For the same reason people like REPLs for other languages. They can be really helpful for debugging code or experimenting with the language to see how things work, especially if you are writing plugins in VimL or highly customizing your config file (which would probably mean you are violating the SRP principal and should break it up into plugins).

  • It would be great to get some examples as videos to show the usefulness of ex-mode. I have never realized it because I can use Linux window manager and vim's visual mode with :!python etc. – Léo Léopold Hertz 준영 Jul 27 '16 at 13:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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