1

I'm trying without success to join lines using a vim

let say I've a text file which looks like this:

HI
Hello 
A, b, c, d,
HI
Hello 
A, b, c, d,
HI
Hello 
A, b, c, d,
HI
Hello 
A, b, c, d,

I want to join all lines that start with HI. to after of Hello. so that it looks like:

Hello HI
A, b, c, d,
Hello HI
A, b, c, d,
Hello HI
A, b, c,
Hello HI
A, b, c, d,
  • Can there be any lines between HI and Hello, or will they always be consecutive? – DJMcMayhem Apr 20 '17 at 18:34
4
:g/^Hello/m-2|j

This is the command you want.

  • g is for the global command

  • ^Hello is the regex

  • m-2 moves the current line to 2 lines above its current position; means swap current line with the above one

  • j joins the current line and the line above, inserting a space between the two. Use j! for no space.

  • | separates the 2 ex-commands

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.