:normal command with a sequence of steps
This is similar to the answer using a macro, but instead of recording the steps, we'll pass them to the
:normal command and use a range to execute the sequence on every line.
We can use the following command:
.,.+3 part is a range and it means from this line until 3 lines below this one, in other words, apply this to the 4 lines starting with this one. You can also type 4, : and Vim will automatically turn that into
:.,.+3 for you.
Yet another option is to use Visual mode to select the lines and then hit :, which Vim will turn into
:'<,'> and that will do the same, assuming the same line range was selected.
The sequence of commands passed to
:normal matches steps 2 through 9 of the answer using a macro exactly.
Using a macro has some advantages, such as giving you visual feedback as you go on the initial line and also making the movement to the next line part of the recording, so making that part more flexible than just a range.
:normal can be pretty useful, for example if you want to turn that into a function or a script you can source, or when seeing the sequence of Vim normal mode commands is more important to you than seeing their immediate effects on the first line you apply the transformation to.
You could quickly adapt that to a more complex sequence of commands. For example, to handle spacing more flexibly around the
=, you could use:
:.,.+3normal ^dWvf=wh"_dA = <Esc>p
Here we're using
vf= to start Visual mode, then select all the way to the
wh to select all whitespace on the right side of it, by moving to the first character of the next word, then one motion left from it. We're then deleting that into the black hole register, so we're not clobbering the variable name we have in the default register. Finally, we're using
A to enter insert mode appending to the line, adding a fixed
= with a single space surrounding it on each side), then leaving insert mode with an
<Esc> before finally putting the variable name that was initially on the left side.
One important point here is that we shouldn't enter
<Esc> as five separate characters
> here, that wouldn't work. Instead, we want to enter a literal
<Esc> keypress here, which we can enter with the sequence of Ctrl+V, Esc, and it will be displayed as
^[, which is the normal representation for an
<Esc> character in Vim.
Alternatively, we could have used the
:execute command to enter the
:normal sequence from a string, in which case we could have used a
\<Esc> sequence inside a double quoted string to enter that particular keystroke. In this case, we'll also need to use
\" for the access to the register:
:execute ".,.+3normal ^dWvf=wh\"_dA = \<Esc>p"
This one is slightly more complex, but its main advantage is that you can easily copy and paste it, since every character is typed literally. It's also convenient for storing into a Vim script file, since then you don't need to insert literal control characters into the file, you can keep the file with printable characters only.