1

Suppose the content in register b is: a^Jbcdefg, ^Jhijkl^J^J, I want to format it by substituting ^J with a space to make it looks like this: a bcdefg, hijkl.

I've consulted many documentations but found no answer. This answer is the closest one on StackExchange.

Here are my commands:

let @b=substitute(@b,'^J',' ','g')
let @b=substitute(@b,'\^J',' ','g')
let @b=substitute(@b,'NL',' ','g')

I thought maybe the linebreak is not ^ appending J so I just copied it from the register when running :reg using the mouse manually, but also got a wrong result.

Can I replace the line break with a space? Or remove the line break?

3

You have to use \n to match newlines:

let @b=substitute(@b,'\n',' ','g')

This also works in s///:

:%s/\n/ /g

If you want to do it the other way around, replace spaces with newlines, it works the way you expect with a register:

let @b=substitute(@b,' ','\n','g')

However, you need to use \r instead of \n in replacement values in s///:

:%s/ /\r/g
  • Thanks very much. I want to know more about this, could you please introduce me the relevant documentation? – Lerner Zhang Jun 21 '15 at 5:48
  • @leoadams There are :help /\n, :help s/\r, and :help NL-used-for-Nul. Not sure what else to tell you. Vim is just weird. – Sato Katsura Jun 21 '15 at 6:00

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.