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
- Select lines with
V(Visual Line) orv(Visual Character) - Press
Jto 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
Jin Normal mode joins the current line with the next one3Jjoins the current line with the next 2 lines (3 lines total)gJin Normal mode joins without spaces
Tips
Jis smart about trailing whitespace and leading spaces — it collapses them into a single space- If a line ends with
.,!, or?,Jinserts two spaces (sentence-aware joining) — disable with:set nojoinspaces - For joining with a custom delimiter, use a substitution instead:
:'<,'>s/\n/, /joins lines with, Jin visual mode is the fastest way to un-wrap a paragraph that was hard-wrapped at 80 columns