By Emacs, how to join two lines into one?
EmacsEditorEmacs Problem Overview
I am new to Emacs. I have googled this but no good answer there. One of them is Ctrl-n Ctrl-a Backspace This works but is stupid. Is there a quick and simple way to join a block of lines into a single line?
Actually, I can use Esc-q to auto-fill a paragraph now, but how could I get it to revert without UNDO?
Emacs Solutions
Solution 1 - Emacs
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.
Solution 2 - Emacs
M-x join-line
will join two lines. Just bind it to a convenient keystroke.
Solution 3 - Emacs
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
Solution 4 - Emacs
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.)
Solution 5 - Emacs
Just replace newlines with nothing.
Solution 6 - Emacs
I like the way Sublime text Join line with Command J so I do it this way:
(defun join-lines (arg)
(interactive "p")
(end-of-line)
(delete-char 1)
(delete-horizontal-space)
(insert " "))
Solution 7 - Emacs
You could define a new command for this, temporarily adjusting the fill width before using the the Esc-q command:
;; -- define a new command to join multiple lines together --
(defun join-lines () (interactive)
(setq fill-column 100000)
(fill-paragraph nil)
(setq fill-column 78)
)
Obviously this only works, if your paragraph has less than 100000 characters.
Solution 8 - Emacs
I use the following function and bind it to 'M-J'.
(defun concat-lines ()
(interactive)
(next-line)
(join-line)
(delete-horizontal-space))
If you prefer to keep your cursor position, you can use save-excursion.
Solution 9 - Emacs
The most simplest way ever:
>1. Select paragraph/lines by M-h
or C-SPC
>2. Press M-q
>3. Witness the Emagics (Emacs Magic)!!
Solution 10 - Emacs
Because join-line
will left one space between two lines, also it only support join two lines. In case of you want to join plenty of lines without one space left, you can use "search-replace" mode to solve, as follows:
C-%
- Query: input
C-q C-j
Enter
- Replace:
Enter
- Run the replacement.
Enter
Done.
Solution 11 - Emacs
"how could I get it to revert without UNDO?":
(defun toggle-fill-paragraph ()
;; Based on http://xahlee.org/emacs/modernization_fill-paragraph.html
"Fill or unfill the current paragraph, depending upon the current line length.
When there is a text selection, act on the region.
See `fill-paragraph' and `fill-region'."
(interactive)
;; We set a property 'currently-filled-p on this command's symbol
;; (i.e. on 'toggle-fill-paragraph), thus avoiding the need to
;; create a variable for remembering the current fill state.
(save-excursion
(let* ((deactivate-mark nil)
(line-length (- (line-end-position) (line-beginning-position)))
(currently-filled (if (eq last-command this-command)
(get this-command 'currently-filled-p)
(< line-length fill-column)))
(fill-column (if currently-filled
most-positive-fixnum
fill-column)))
(if (region-active-p)
(fill-region (region-beginning) (region-end))
(fill-paragraph))
(put this-command 'currently-filled-p (not currently-filled)))))
(global-set-key (kbd "M-q") 'toggle-fill-paragraph)
Solution 12 - Emacs
From EmacsWiki: Unfill Paragraph
;;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph
(defun unfill-paragraph (&optional region)
"Takes a multi-line paragraph and makes it into a single line of text."
(interactive (progn (barf-if-buffer-read-only) '(t)))
(let ((fill-column (point-max))
;; This would override `fill-column' if it's an integer.
(emacs-lisp-docstring-fill-column t))
(fill-paragraph nil region)))
Solution 13 - Emacs
A basic join of 2 lines:
(delete-indentation)
I like to line below to be joined to the current without moving the cursor:
("C-j" .
(lambda (iPoint)
"Join next line onto current line"
(interactive "d")
(next-line)
(delete-indentation)
(goto-char iPoint)))
Solution 14 - Emacs
This one behaves like in vscode. So it add space only if join line consisted something else than whitespace. And I bind it to alt+shift+j
.
Shorter version based on crux-top-join-line:
(global-set-key (kbd "M-J") (lambda () (interactive) (delete-indentation 1)))
Longer version based on https://stackoverflow.com/a/33005183/588759.
;; https://stackoverflow.com/questions/1072662/by-emacs-how-to-join-two-lines-into-one/68685485#68685485
(defun join-lines ()
(interactive)
(next-line)
(join-line)
(delete-horizontal-space)
(unless (looking-at-p "\n") (insert " ")))
(global-set-key (kbd "M-J") 'join-lines)
Solution 15 - Emacs
Two ways come to mind:
-
Once you think of it, the most obvious (or at least easiest to remember) way is to use
M-q format-paragraph
with a long line lengthC-x-f 1000
. -
There is also a built-in tool
M-^ join-line
. More usefully, if you select a region then it will combine them all into one line.