Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the :g command in vim with multiple actions

How can I use something like this?

:g/^$/kJ

Here kJ are two commands, instead of just one (like 'd')

My concrete example: I have multiple lines looking like this

queryBuilder
    .append("xyz");

and I want to make them look like this:

queryBuilder.append("xyz");

So what I want to do for each line is

:g/^[\t]*\..*$/kJx

which matches the correct pattern but seems to execute only k.

Are other vim commands suitable here? How would you perform this task?

like image 967
kadrian Avatar asked Jul 16 '12 20:07

kadrian


People also ask

How do I run multiple commands in vim?

You can execute more than one command by placing a | between two commands. This example substitutes for htm, then moves on to JPEG, then GIF. The second command (and subsequent commands) are only executed if the prior command succeeds.

What does the G key do in vim?

gg will move you to the first line in the file, and G will move you to the last line in the file. Alternatively, if you type nG where n is a number, you'll jump to that to that line.

What does Ctrl d do in vim?

You will see list of commands that matches characters in front of the cursor by pressing CTRL-D in the command mode. For example, if you pressed :se in normal mode, then pressed CTRL-D , you will see list of commands that start with se .

What is EX mode vim?

The Ex mode is similar to the command line mode as it also allows you to enter Ex commands. Unlike the command-line mode you won't return to normal mode automatically. You can enter an Ex command by typing a Q in normal mode and leave it again with the :visual command.


1 Answers

Add the normal instruction to execute all of them, like:

:g/^[\t]*\..*$/normal kJx
like image 59
Birei Avatar answered Oct 12 '22 11:10

Birei