Ex Mode

Ex is one of the original line editors for Unix, before people could edit and view the full state of a document in a visual editor.

Ex can be used on Unix-like command lines but can also be used in Vim (called Ex Mode) by pressing : (when you are in Normal Mode) and entering an Ex command in Vim’s prompt.


Some useful commands

:w :q :wq
These are widely used and you may have used them for years without realizing they are Ex commands.

:%d
This will delete all of the text in the current buffer. Much more efficent than the vim motion of gg[S-v]Gx.

:r filepath
This will read filepath and then write that file’s contents as input into your current buffer.


Pattern Matching

Two of the most useful commands in Ex are for pattern matching. Note that the Vim motions of / and ? use VI’s search mechanism were as :s and :g use Exs.

:g/pattern
Will find all matches for the pattern in the current buffer.

:s/old/new
Replaces the old pattern for the new new.

:s/old/new/g
Will change every occurence in the file.

:%s/old/new/g
This will substitute on all the content of the file. % signifies the full content of the file.

:50,100s/old/new/g
All Ex commands can receive an optional line range. This command will replace every pattern that matches old to new from lines 50 to 100 of our current buffer.

:%s/old/new/gc
Adding the c flag at end will give us a confirmation dialog on each find and replace.


Context-sensitive

:g/pattern/s/old/new/g

This is a slightly more complex syntax for global replacement.

The first g tells the command to operate on all lines of the file.

The pattern identifies the lines on which a substition should take place.

On the lines where there is a match, Ex will subsitue new for old.

The last g instructs Ex to make the substituion globally.

For example take the following:

:g/<keycap>/s/Esc/ESC/g

This will replace any pattern that matches Esc that is preceded by <keycap> and uppercase it to ESC.


Regular Expressions

For the most part regular expressions will also work with other Unix programs suchas as grep, sed and awk.

CharDescription
.Matches any single character except a new line, (spaces are also treated as characters).

For example p.p matches character strings such as pop, pip and pcp.
*Matches 0 or more of the single character that immediately precedes it.

For example bugs* will match bugs, bug, bugs and buggs.

The * can also be stacked, .* will match any number of character.
^When used at start of a pattern will match the first part of a string.

^... matches the first 3 characters of a line.

When not used at start, ^ stands for itself.

And when used in brackets [] it stands for negation.
$Same as ^ above but with $ we match the end of a regular expression and when not used at the end of a string it also stands for itself.
\Transforms meta-characters to normal characters.

\. would match a period and \* would match an asterisk.

To match a literal backslash we use \\.
[]This represents a character set. For example [ABC] matches A, B or C and p[aiou]t would match pat, pet, pit or put.

A range can also be used such as [A-Z], [a-z] or [0-9].

We can also combine ranges and other characters for more advanced matching, such as [:;A-Za-z()] which will match 4 different punctuation marks, plus all letters.

Note: Most meta-characters lose their special meaning inside brackets. Within brackets the 3 we always need to escape are \, - and ].
\(<str>\)This is called a capture group. For example:

%s/\(That\) or \(this\)/\2 or \1/ would change a string of

That or this to this or That

We can also use the capture group as a multiplier in the matching string:

:s/\(abcd\)\1/alpahbet-soup/ changes

abcdabcd into alphabet-soup.
\< \>Matches characters at the beginning or end of a word and can be used seperately or together.

For example the expression \<ac will match only words that begin with ac.
&This is variable that is the match of the pattern that we searching for.

For example %s/cat/the &/g will replace every instance of cat with the cat.

Or to surround every line from 1 to 10 with parentheses, we could use :1,10s/.*/(&)/
~This will reference the the last replacement used.

So if we first executed :s/thier/their/g we can repeat the replacement string on another match with :s/his/~/g.

We can also combine `:g with there ex commands like

  • :d for delete
  • :mo for move
  • :co for copy