Vim in Ex mode 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.
vi is the visual mode for
ex therefore Vim Ex Mode is just emulation of
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 are actually two modes: 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:
vi. Ex is a super‐
ed, with the most notable extension being a display editing
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 (they're not designed to work with multiple lines) while
vim is buffer oriented - you can move forward and backward in the file as you like which makes it so powerful.
sed is a Stream EDitor, not a file editor.
Nevertheless, people seems to abuse it for trying to edit files and the truth is that it doesn't edit files. Secondly its options such as in-place (
-i) are non-standard FreeBSD extensions and may not be available on other operating systems. So if you want to avoid unportable code, I/O overhead and bad side effects (such as destroying symlinks) you should use
ex which is the standard UNIX command-based editor (along with
Other things which I find useful in Ex mode is to use it as a playground (similar to
python console) where you can execute many commands in a row, working/debugging regular expressions, checking vim configuration, executing external commands or working with registers, etc.
let @d = '<td></td>'
let @r = '<tr>' . repeat(@d, 5) . '</tr>'
let @t = '<table>' . repeat(@r, 5) . '</table>'
which is more easier in Ex mode than in normal mode (where you can see only your last command).
I've the following aliases in my
alias trim="ex +'bufdo!%s/\s\+$//e' -scxa"
alias retab="ex +'set ts=2' +'bufdo retab' -scxa"
bufdo is not
ex POSIX-compliant method (as per manual), so then you can consider using it with
find instead. The
! is used to force switching the buffers without saving (otherwise warning is generated).
The first one I'm using to trim the trailing spaces in all my source files, e.g.:
The second one converts all tabs into spaces (recursively), e.g.:
For me using
retab is enough, but there are some downsides described in here. Add extra
-V for increased verbosity output.
Note that above examples using zsh/bash4 globbing (
**), so make sure your shell supports it and it's enabled.
For more practical examples (like parsing html files), check:
Also learn further about Ex-mode at:
q:are quite different. You're only referring to
q:specifically, and mentioned it as, among the two key sequences, the one you have the most issue with. And two of the answers here may be useful to people who are searching for it by the correct term.
q:do, you'll probably consider them some of the most useful commands in Vim. And yes, you already know how to use them, you just don't know yet what they do. Watch this screencast to find out.