Learn Practice Daily golf Reference

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.

The ninja pulling a big zip closed down a stack of short clay line strips, the strips above the zip already merged into one long strip, a J keycap on the bench.

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.

Before
Tonight's special is slow-cooked
carnitas with pickled onions and
J
Tonight's special is slow-cooked carnitas with pickled onions and
The line break at the end of the top line becomes a single space, and the line below moves up to sit after it
Try it here

Mend the special

The cursor is on the first of the three lines.

Pull the second line up with Jdojo.ts
1Tonight's special is slow-cooked
1carnitas with pickled onions and
2salsa verde, wrapped to order.
NORMALLoading the editor…0.0sLn 1, Col :22
Open this text in the playground

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.

Try it here

One line for the order

This call fits on one line, and the rest of the file writes it that way.

Join all four lines with 4Jorder.js
1addToOrder("burrito",
1 "carnitas",
2 "extra salsa",
3 "no onions");
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

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.

Try it here

Fix the link

The cursor is on the first half of the link.

Glue the two halves together with gJdojo.ts
1Order online at https://byteburrito.exa
1mple/order and skip the queue
NORMALLoading the editor…0.0sLn 1, Col :17
Open this text in the playground

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.

Check yourself

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?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1Sides: chips
1 guacamole
2 elote
3Drinks: horchata
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

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.

Try it here

Sold out today

The cursor is on the first item.

Select the first two items with V j, press :, type s/$/,/ and press ↵dojo.ts
1carnitas
1al pastor
2guacamole
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This 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.

Try it here

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.

Select the first two items with V j, press :, type s/\n/, / and press ↵dojo.ts
1carnitas
1al pastor
2guacamole
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

KeysJoins
Jthe line below onto this one, its indent replaced by one space
4Jfour lines in all, this one and the three below
gJthe line below exactly as it is, with no space added and its indent kept
V, j, then Jevery selected line into one (gJ for no spaces)
:jthis line and the next, like J
:5,9jlines 5 to 9
:%jevery line in the file
:%j!every line in the file, with no spaces added
:'<,'>s/$/,/ then Ja 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