Tuesday, March 2, 2021

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the Ex Mode where you can continually type your commands using Vim's built-in Ex-compatibility mode.
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.

The Ex mode is also part of a broader group of advanced capabilities which we'll discuss in future posts.

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
From there, you can start typing your commands. For example, highlighted in yellow are three commands run in the same Ex session:

While in Ex mode, keep entering your Ex commands

Ex mode from the shell

Ex mode can also be accessed from the shell via the ex command (or vim -e). Running it, should result in a screen similar to:
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 awksed 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
Keep tuned as we will provide more examples of the above use cases in future blog posts.

Learning More

Ready to learn more about the Ex mode? Open its dedicated manual with:

:h ex-mode

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:

See Also

Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts