Photo by
Alex Knight
on
Unsplash |
Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Ex Mode. This is the mode where you can commands in batch and even emulate the old Ex text editor.
What's Ex Mode?
If you learned about the Command-line mode you already know how to enter Ex commands in Vim. Turns out that the Ex mode is an extension of that mode and allows us to run not only single commands but also serves as a built-in batch processing tool for Vim.
It's also worth pointing out that Ex is a text editor, predecessor of Vim, and from which Vim (and vi) got a lot of inspiration from. Vim still today supports a direct Ex emulation (via the Q command) which is less and less used.
Entering Ex mode
To enter the improved Ex mode from Vim, press either gQ in Normal mode. You should see a message similar to:
Type gQ to get to Ex mode |
While in Ex mode, keep entering your Ex commands |
Ex mode from the shell
Vim started in ex mode |
Getting to Normal Mode
To get back to Normal mode from Ex mode, enter the :visual c(or :vi for short).
Quitting Ex Mode
To quit Ex mode, enter the already known :q command.
When to use the Ex mode?
Advanced Vimmers prefer using the Ex for batch processing since they can combine Vim's awesome features (such as buffers and regular expressions) in more powerful ways than if they had to perform the same operations by combining tools like awk, sed and others.
Apart from batch processing, other more advanced use cases are:
- editing multiple files non-interactively (for example, as part of the script)
- an alternative for slow connections
- mappings and abbreviations are disabled
- restricted access to keys such as Escape or Control
Learning More
Ready to learn more about the Ex mode? Open its dedicated manual with:
Mode-specific help
If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:
What | Prepend | Example |
Normal mode command | :help x | |
Visual mode command | v_ | :help v_u |
Insert mode command | i_ | :help i_<Esc> |
Command-line command | : | :help :quit |
Command-line editing | c_ | :help c_<del> |
Vim command argument | - | :help -r |
Option | ' | :help 'textwidth' |
Regular expression | / | :help /[ |
We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.
Conclusion
On this post we continued in our Vim journey by learning a bit more about Vim's Ex mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.
Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.
Recommended Reading
Since the use cases for Ex mode are more advanced, we recommend reading the following articles:
- How to edit files non-interactively (e.g. in pipeline)?
- How can I replace a string with another string in a variable, a stream, a file, or in all the files in a directory?
- Learning the vi Editor/Vim/Modes: Ex-mode
- Does Ex mode have any practical use?
See Also
- Switching modes in Vim
- Vim - Normal Mode
- Vim - Insert Mode
- Vim - Visual Mode
- Vim - Blockwise Visual Mode (aka vertical selection)
- Vim - Replace Mode
- Vim - Command-Line Mode
- Vim - Select Mode
- Why use Vim
- How to get started with Vim
- Learn to learn Vim
- Vimtutor - Your first Vim tutorial
- Vim Modes - All you need to know