rnso
rnso

Reputation: 24535

Shift blank line after this line to before this line in Vim

I have following text:

A simple line
TITLE1:

Another usual line
TITLE2:

More usual lines here
TITLE3:

Last line of this sample text.

I want to convert above to:

A simple line

TITLE1:
Another usual line

TITLE2:
More usual lines here

TITLE3:
Last line of this sample text.

Hence, I want to remove the blank line that comes after the TITLE lines and instead add a blank line before the title lines. The TITLE lines are all identfied by ending with ':'.

I tried following code

:%s/(.+):\r/\n\1:/g

but it does not work.

Upvotes: 0

Views: 52

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11790

:g/TITLE/ m+1

How does it work?

: ............... command
g ............... global
/TITLE/ ......... all lines with TITLE
m+1 ............. move to the next line

Upvotes: 1

Kent
Kent

Reputation: 195029

If I understand your requirement right, I would go with :g//{cmd} instead of :s/../../ :

g/:$/norm JO

If you don't want to have the space after the :, add a g before J

explanation:

:g/pat/norm JO
  • for all lines matching pat, do normal mode command J and O.
  • J does join, which means "removed" the next line break
  • O adds an empty line before the current line.

You can move your cursor on the TITLE line press JO see what happens.

If you need further explanation/details, pls read:

:h :g
:h :norm
:h J  (or :h gJ)
:h O

Upvotes: 2

Related Questions