The global search a.k.a. “the g command” performs a global search, through the file and marks those lines for you to do something with them (default is the p command i.e. print).
Example:
will print every line containing the pattern enclosed within the slashes. If you want to delete those lines append that instruction with the delete command or its contracted form d:
you could also combine it with the move, copy or even the substitution command. Substitution example:
:g/riding my bike/s/riding my bike/riding my ferrari/g
will substitute “riding my bike” for “riding my ferrari” on those lines that match the pattern “riding my bike”. Repeating the pattern seems silly, and it really is. You can omit the second pattern (in the substitution command) like this:
:g/riding my bike/s//riding my ferrari/g
which (the empty pattern) means “use the last pattern used”, in this case, the one belonging to the g command (“riding my bike”).
You could also use the hold buffer functionality (a.k.a. capturing group):
:g/\(riding my \)bike/s//\1ferrari/g
The hold buffer is used in the replacement text as \n with n varying from 1 to 9. This 1-9 hold buffer index is based on the order that the parenthesis appears on the pattern. Such parenthesis represent the boundaries of the hold buffer (capturing group). In the example, since we have only one such buffer, we use it in the replacement text as \1. This way we save typing.
An important note to the form of the g command combined with substitution with empty pattern (same as the g command’s) i.e. the g/pattern/s//replace/g form, is that it is equivalent to that substitute command applied for all lines:
The % on the start is the symbol that is equivalent to 1,$ i.e. from the first (1) to the last ($) lines (including them) or put it simple “the whole file”. Both these are what is known as ex’ line addressing. This subject is out of this post’s scope (vi/vim documentation and books cover this subject well).