How to join lines in Vim
In Normal mode, press J to pull the line below up onto the one the cursor is on, with one space between them. 3J makes one line out of three, and gJ joins without adding the space.

You pasted tonight's special from the chef's email into the text file the specials board reads from, and it brought the email's line breaks with it. Now one sentence sits on three short lines, and the board will show it as three. It needs to be one line again.
Tonight's special is slow-cookedcarnitas with pickled onions andTonight's special is slow-cooked carnitas with pickled onions andMend the special
The cursor is on the first of the three lines.
This editor needs a keyboard. Read on, and play it at your computer.
Join several lines at once
Put a count in front and J joins that many lines, counting the one you're on. 4J turns four lines into one in a single go.
When it pulls a line up, J also drops the spaces at its start. That makes it good at putting back a function call that someone spread over several lines, because the indent of each argument disappears and only the one space is left. If the line below starts with a ), no space goes in before it.
One line for the order
This call fits on one line, and the rest of the file writes it that way.
This editor needs a keyboard. Read on, and play it at your computer.
Join lines without a space
Sometimes the space is the wrong thing to add. You copied a link out of a terminal that wrapped it at the edge of the window, and the address now breaks in the middle of a word.
gJ joins the line below exactly as it is. It adds no space and leaves any spaces at the start of that line where they were.
Fix the link
The cursor is on the first half of the link.
This editor needs a keyboard. Read on, and play it at your computer.
Join a whole block or the whole file
If you'd rather see the lines than count them, select them with V and j, then press J. Every selected line becomes one.
On the command line, :j (short for :join) does what J does, and a range in front of it says which lines. :%j joins every line in the file, wherever the cursor is, and :%j! does the same without adding spaces, like gJ.
Join what you selected
The cursor is on chips, in Normal mode. You press V, j, j and then J. What does the first line say now?
Join a list into one line with commas
The kitchen's sold-out list is one item per line, and the board out front wants it on one line with commas. J only puts spaces at the seams, so give every item except the last its comma first, then join.
Select those lines with V and j, then press :. Vim fills in :'<,'> for the selection, and you type s/$/,/ after it. The $ stands for the end of the line, so each selected line gets a comma on its end. The cursor lands on the last line that changed.
Sold out today
The cursor is on the first item.
s/$/,/ and press ↵dojo.tsThis editor needs a keyboard. Read on, and play it at your computer.
Commas and the join in one command
A substitute can replace the line break itself. In a pattern, \n matches the end of a line, so s/\n/, / swaps each line ending for a comma and a space, and the next line comes up to meet it.
Select every line except the last, so the last item gets no comma after it. On those selected lines, :'<,'>s/\n/, / joins the whole list in one go. Run on the whole file as :%s/\n/, /, it also gives the last line a comma and a space at its end, which you then delete.
Sold out tomorrow
The same three items are sold out again. This time, do it in one command. The cursor is on the first item.
s/\n/, / and press ↵dojo.tsThis editor needs a keyboard. Read on, and play it at your computer.
| Keys | Joins |
|---|---|
| J | the line below onto this one, its indent replaced by one space |
| 4J | four lines in all, this one and the three below |
| gJ | the line below exactly as it is, with no space added and its indent kept |
| V, j, then J | every selected line into one (gJ for no spaces) |
:j | this line and the next, like J |
:5,9j | lines 5 to 9 |
:%j | every line in the file |
:%j! | every line in the file, with no spaces added |
:'<,'>s/$/,/ then J | a list with commas, once the substitute has put one on the end of each selected line |
:'<,'>s/\n/, / | the selected lines and the one after them, with a comma and a space at each seam |