0

I have these lines;

Question1. Is this true?
a. Yes
b. No
Question2. How is the weather
a. Good
b. Bad
Question3. The sky is
a. Dark
b. Blue

I wanted to change it to;

Question1. Is this true?
a. Yes
b. No

Question2. How is the weather
a. Good
b. Bad

Question3. The sky is
a. Dark
b. Blue

Is it possible to do this with a one liner command on Vim(version 81)?

3

You can make use of the :g command. There are probably already answers here on how to use :g and there is also this famous wiki article on the same topic

In short, you simply run:

:g/^Q/:-put =''

which basically means, for each line starting with Q put an empty line above it. Alternatively you could use:

:g/^b/:pu =''

which would add an empty line below each line starting with b.

1
  • Nice one! Also possible to use put! ='' to put before the matched line, which doesn't require the use of - to match the line above. And typically I tend to avoid the optional : in the middle of a command, like here, I think it might look unclear to some (is that part of how :g works?) Though in this case, it might help see that the - is an additional address to :put rather than part of the pattern given to :g (though wouldn't they work the same?)
    – filbranden
    Nov 24 '20 at 15:24
3

To follow up on @Christian Brabandt's answer, you can use the O key (see :help O) and do:

:g/^Q/norm O

norm allows to run a key command (O, dd, .....) in a command.

:g can take a range also, so if you don't want to do it on the first question, you could do:

`:2,$g/^Q/norm O

Assuming that your file contains only that text, otherwise, the range needs to be adjusted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.