unix - Delete newline in Vim

ID : 10031

viewed : 19

Tags : unixvimshellsshunix

Top 5 Answer for unix - Delete newline in Vim

vote vote

94

If you are on the first line, pressing (upper case) J will join that line and the next line together, removing the newline. You can also combine this with a count, so pressing 3J will combine all 3 lines together.

vote vote

81

Certainly. Vim recognizes the \n character as a newline, so you can just search and replace. In command mode type:

:%s/\n/ 
vote vote

70

While on the upper line in normal mode, hit Shift+j.

You can prepend a count too, so 3J on the top line would join all those lines together.

vote vote

61

As other answers mentioned, (upper case) J and search + replace for \n can be used generally to strip newline characters and to concatenate lines.

But in order to get rid of the trailing newline character in the last line, you need to do this in Vim:

:set noendofline binary :w 
vote vote

58

J deletes extra leading spacing (if any), joining lines with a single space. (With some exceptions: after /[.!?]$/, two spaces may be inserted; before /^\s*)/, no spaces are inserted.)

If you don't want that behavior, gJ simply removes the newline and doesn't do anything clever with spaces at all.

Top 3 video Explaining unix - Delete newline in Vim

Related QUESTION?