Last time I showed how to run a normal mode move across several lines using the command line. What happens when you want to run a normal mode movement in just a few specific lines?

You can use the powerful Vim g command.

:g/if/normal J

In this example I want to find all lines that contains “if” and perform the normal mode movement J, bringing up the line below it.

Sounds easy, right?

Lets make it more interesting. I’ll do the same thing — turning if statements into one-liners — but this time I’ll wrap it in curly brackets.

:g/if/normal A {^CJA }

Again, this is a normal mode move being applied to lines matching an specific pattern. We are opening a curly brackets in the end of the line (A {), getting back to normal mode (ctrl+v ctrl+c or ctrl+v esc), bringing up the line below it (J) and adding the closing curly brackets (A })

If you are wondering, the ctrl+v allows us to type a non-digit literally in command line (in our case the ESC or Ctrl+C).