How do I join a specific number of lines at once without pressing J multiple times?
Answer
{count}J
Explanation
The J command joins the current line with the line below, adding a space between them. Prefixing it with a count joins that many lines into one in a single keypress — no need to repeat J or enter visual mode.
How it works
J— join the current line and the next line (2 lines total){N}J— join N lines starting from the current line into a single line- Vim inserts a single space between joined lines (or two spaces after sentence-ending punctuation, configurable with
:set joinspaces) - The count N includes the current line, so
3Jjoins the current line plus the 2 lines below it
Example
Given these lines with the cursor on the first:
foo
bar
baz
qux
Pressing 3J joins the first three lines:
foo bar baz
qux
Tips
Jwith no count is equivalent to2J(joins 2 lines)- Use
gJto join lines without inserting any space between them - In visual mode,
Jjoins all selected lines — useful when you have a non-contiguous block selected - The Ex command
:[range]joinachieves the same with a line range::.,.+2joinjoins 3 lines - Combine with a count and dot to rapidly collapse blocks:
3Jthen.to keep joining groups