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

How do I join multiple selected lines into one line using visual mode?

Answer

J (in visual mode)

Explanation

Select lines in Visual mode and press J to join them into a single line. Vim removes the line breaks and inserts a single space between each joined line (or no space if the line ends with whitespace). gJ joins without adding any spaces.

How it works

  1. Select lines with V (Visual Line) or v (Visual Character)
  2. Press J to join them
Command Effect
J Join with a space between lines
gJ Join without any space

Example

Given:

Hello
World
from
Vim

Select all 4 lines (ggVG), press J:

Hello World from Vim

With gJ instead:

HelloWorldfromVim

Normal mode variants

  • J in Normal mode joins the current line with the next one
  • 3J joins the current line with the next 2 lines (3 lines total)
  • gJ in Normal mode joins without spaces

Tips

  • J is smart about trailing whitespace and leading spaces — it collapses them into a single space
  • If a line ends with ., !, or ?, J inserts two spaces (sentence-aware joining) — disable with :set nojoinspaces
  • For joining with a custom delimiter, use a substitution instead: :'<,'>s/\n/, / joins lines with ,
  • J in visual mode is the fastest way to un-wrap a paragraph that was hard-wrapped at 80 columns

Next

How do I run a search and replace only within a visually selected region?