By Emacs, how to join two lines into one?

Place point anywhere on the last line of the group of lines that need joining and call

M-^

repeatedly until all the lines are merged.

Note: It leaves one space between all of the now joined lines.


The Emacs conventional name for "join" is "fill". Yes, you can join two lines with M-^ -- and that's handy -- but more generally you'll want to join n lines. For this, see the fill* commands, such as fill-region, fill-paragraph, etc.

See this for more info on selecting things which can then be filled.

Also, you can join multiple lines with M-^ by selecting those lines first. (Note that the universal argument does not work with this.)


Multiple Cursors combined with M-^ will collapse all selected lines into one with all extraneous white-space removed.

For example to select an entire buffer, invoke multiple cursors mode, collapse into one line, and then disable multiple cursors mode:

C-x h
M-x mc/edit-lines
M-^
C-g

M-x join-line will join two lines. Just bind it to a convenient keystroke.

Tags:

Emacs

Editor