vimtricks.wiki Concise Vim tricks, one at a time.

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 3J joins 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

  • J with no count is equivalent to 2J (joins 2 lines)
  • Use gJ to join lines without inserting any space between them
  • In visual mode, J joins all selected lines — useful when you have a non-contiguous block selected
  • The Ex command :[range]join achieves the same with a line range: :.,.+2join joins 3 lines
  • Combine with a count and dot to rapidly collapse blocks: 3J then . to keep joining groups

Next

How do I create new files and directories directly inside Vim's built-in file browser without leaving Vim?