:substitute) is one of the first commands any Vim user learns. There are several similar commands that are more generic. Like
:s, these commands can take a range; however, unlike
:s, there is no
%variant, as these commands are global by nature.
These commands can be seen as a simpler, less general-purpose alternative to macros that takes less keypresses.
Instead of substituting strings matching the first part with the second, runs the (command mode) command on strings matching the first part.
:g, but targets strings that do NOT match.
g/pattern/_dputs deleted lines in the null register, which has no performance cost.
This "scopes" the substitution to matching lines (for example, to scope your substitution to comments).
Note that we are yanking to an uppercase register, meaning we append matches. If we did
y a, each yank would overwrite the previous yank.
This applies the normal mode instruction (here, "go to start and insert hello") to matching lines.
g/pattern/exec "norm!" "A\r"
:norm, you might notice that you can't insert linebreaks -- and
<CR>is literally printed as the strings "\r" and "".
:exec(ute)lets us actually pass in commands as strings to be executed, like
This is a somewhat meta pattern that follows the last example. It executes the two separate commands
"A\r"to the pattern. Before execution, the second is evaluated to "append carriage return."