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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
Vim in Ex mode (also known as
ex) is useful when:
Editing files non-interactively is the most common usage and people using it in similar way as
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)
Here is simple example of changing
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
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.
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
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
ex -s table.html < table.vim
This will work on *nix like systems with curl installed. Add
-Vto see the script in action.
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
vim -E -s bob.html <<-EOF :%substitute/home.html/index.html/ :update :quit EOF
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.
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).