Search code examples
searchvimreplacebackspace

Using backspace on VIM search and replace


What character do I use for the newline on Vim search and replace commands?

I'm trying to make this:

1, 
2, 
3, 
4, 
5 

to this: 1, 2, 3, 4 ,5

So i thought of writing something like: :%s/$/\b/g

But it didn't work even if I set: :set magic. How can I achieve that?


Solution

  • Newlines are represented by \n.

    So, with such a simple example, you can replace every newline with:

    %s/\n//g
    

    You can replace each comma, followed by optional whitespace until the end of a line with a space, like so:

    :%s/,\s*$\n/, /g
    

    Of course, the J operator will most likely suit your needs just fine as well (try Jip within the block you wish to concatenate. Or to automatically line wrap per your textwidth setting: gqip.