1. Overview

Vim is a powerful and versatile text editor that offers many features and modes for editing different types of files. One of these modes is Ex mode. This is a special mode that enables us to execute commands in a non-interactive way. It can be very useful for enhancing productivity and efficiency in Vim, especially with complex or repetitive tasks on multiple files or lines of text.

In this tutorial, we’ll explore what Ex mode is and how it differs from other modes in Vim. In addition, we’ll discuss how to access and use the mode and some of its practical applications.

2. Ex Mode

Ex mode in vi emulates the behavior of the ex editor, which is the direct predecessor of vi and vim. Additionally, in Ex mode, we can enter commands that manipulate the current buffer or perform other actions:

  • saving
  • quitting
  • editing another file
  • running external commands
  • running most built-in commands

Ex mode is different from Normal mode and Command-line mode, which are perhaps the two most common modes in Vim. However, the main difference is that we don’t immediately see the effects of our commands on the screen. Instead, we have to press Return to execute each command and see the results. Consequently, this makes Ex mode less interactive and more suitable for batch processing or scripting.

3. Accessing Ex Mode in Vim

We can enter Ex mode from Normal mode by pressing Shift-Q. This switches to Ex mode and displays a prompt at the bottom of the screen where we can enter commands:

:

To exit Ex mode and return to Normal mode, we can type vi or visual and press Return.

Alternatively, we can start Vim in Ex mode by using the -e or -E options when launching Vim from the command line:

$ vim -e

The -e option starts Vim in standard Ex mode. Alternatively, the -E option goes to improved Ex mode. Standard Ex mode tries to emulate the behavior of the original Ex editor. Conversely, improved Ex mode allows for more advanced commands than the vi-compatible Ex mode, and behaves more like typing :commands in Vim.

Additionally, mappings, shorthands, and abbreviations aren’t used in standard Ex mode while they’re used in improved Ex mode as they’re defined in Vim.

4. Some Basic Ex Mode Commands

Ex mode commands are similar to Command-line mode commands. However, they have some differences in syntax and functionality.

4.1. :normal command

We can use the normal command to execute a Normal mode command on each line in the buffer or in a specified range of lines.

For instance, let’s indent all lines in the buffer by four spaces:

:%normal i

i is the command to move to Insert mode in Vim. Therefore, this entry inserts four spaces at the beginning of each line in the buffer. Notably, if we enter the above command without the %, we’d add four spaces to the current line alone.

Of course, we can also use the normal command over a specified range of lines. For instance, let’s append a semicolon at the end of lines one through five in the current buffer:

:1,5normal a;

This command executes a; for the specified lines. Consequently, the cursor moves to the end of each line and inserts a semicolon.

4.2. :global/pattern/command

We can use the global command to execute an Ex command on all lines that match pattern.

For instance, let’s delete all lines that contain foo in the buffer:

:g/foo/d

d in the above command deletes a line. The latter is selected based on the pattern foo from all lines in the buffer globally.

4.3. :vimgrep/pattern/files

vimgrep can search for pattern in multiple files and populate the quickfix list with the matches.

For example, let’s find all occurrences of foo in all .txt files in the current directory:

:vimgrep/foo/*.txt

This entry searches for foo in all .txt files in the current directory and populates quickfix with the matches. We can then use :cnext and :cprev to navigate through the matches.

4.4. :cdo command

cdo executes an Ex command on each entry in the quickfix list.

For instance, we can replace foo with bar in all .txt files that contain foo in the current directory:

:vimgrep/foo/*.txt
:cdo %s/foo/bar/g | w

This operation runs %s/foo/bar/g | w on each entry in the quickfix list. That is, we replace foo with bar and save each file.

4.5. :sort

We can use the sort command to sort the lines in the buffer or in a specified range of lines alphabetically:

:%sort

Sorting can be helpful when dealing with databases or dictionaries.

5. Practical Applications of Ex Mode

We can use Ex mode for editing and manipulating text efficiently, searching and replacing text patterns, and automating tasks. Let’s look at how to use Ex mode commands to solve some practical problems and everyday tasks.

5.1. Editing and Manipulating Text

We can use Ex mode to perform bulk editing and text substitution on multiple lines or files. For instance, we can change all occurrences of foo to bar in the current file:

:%s/foo/bar/g

This command searches for foo in the current file and changes all occurrences to bar.

Also, we can do the same for multiple files:

:argdo %s/foo/bar/g

Notably, before this command, we usually execute args:

:args *.txt

Thus, we set the argument list to all .txt files in the current directory.

Also, we can use Ex mode to perform batch operations on a range of lines using Ex range commands. For instance, let’s delete lines 10 to 20 in the current file:

:10,20d

Also, we can copy the same lines to line 30 in the current file:

:10,20t30

This way, we can copy or move lines, manipulating a file without scrolling through it or selecting data manually.

5.2. Searching and Replacing Text Patterns

We can use Ex mode to perform advanced search and replace operations using Ex commands. For instance, let’s find all lines that contain foo and replace bar with baz on these lines:

:g/foo/s/bar/baz/g

This entry substitutes all occurrences of bar with baz on all lines that contain foo in the current file.

We can also use Ex mode to harness the power of regular expressions for searching and replacing text patterns. For example, let’s find all lines that start with a number and end with a punctuation mark:

:g/^\d.*[.,;:?!]$/p

Using such techniques, we can change certain aspects of a file or its format quickly.

5.3. Automating Tasks

Furthermore, we can use Ex mode to create and execute macros that can further automate repetitive tasks.

For instance, let’s create a macro that inserts a comment header at the beginning of each file:

:let @a = "ggo/* Comment header */\n*/\n"

Thus, we use register a to store a macro made up of three parts:

  • gggo to the first line of the file
  • o – create a new line above the current line and enter Insert mode
  • /* Comment header */\n*/\n – insert the comment header text and two newlines

Then, we can execute the macro on any file in Normal mode:

@a

So, we insert the header text at the beginning of the file:

/* Comment header */
*/

Notably, the stars aren’t part of the Ex command syntax, they’re just part of the text that the macro inserts.

6. Conclusion

Ex mode offers a range of commands that enable us to manipulate text efficiently, search and replace patterns seamlessly, and even automate complex and repetitive tasks. This can help streamline our workflow and save valuable time and effort.

In this article, we’ve explored what Ex mode is and how it differs from other modes in Vim. Also, we discussed how to access and use it. Finally, we look at some of the practical applications of Ex mode in Vim.