55

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?

Alan
  • 1,889
  • 2
  • 18
  • 30
jcadam
  • 983
  • 1
  • 10
  • 18

15 Answers15

197

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.

Ray
  • 187,153
  • 97
  • 222
  • 204
39

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

pgs
  • 13,385
  • 2
  • 25
  • 17
  • Yes. It works. However, it is of no use to a block of several lines(3 lines or more). Any useful command for this purpose? – jcadam Jul 02 '09 at 05:48
  • 8
    But if you go to the last line of the block and hit M-^ (the key binding for join-line) several times you will have the same effect. Goes pretty fast, so unless you have hundreds of lines to join I would prefer it over the fill-column hack. Otherwise do as Tal suggests, mark the block and replace newline (C-Q C-J) with nothing. – danielpoe Jul 02 '09 at 06:37
  • I have tried to bind join-line to M-1. It works fine also. As you you suggested, many solutions here for the purpose, so I'll try to use them all in a flexible way. Thank you very much! – jcadam Jul 03 '09 at 08:31
  • 2
    This is exactly the same thing as pressing `M-^`, because `join-line` is aliased to `delete-indentation`, which is bound to `M-^`. – Vicky Chijwani Jan 12 '13 at 03:27
  • With my emacs version (29.0.50) this worked for more 2 lines. I highlighted all the lines then ran M-x join-line – krm Apr 14 '23 at 10:22
12

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
huntar
  • 1,072
  • 11
  • 9
11

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.)

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
9

Just replace newlines with nothing.

Tal Pressman
  • 7,199
  • 2
  • 30
  • 33
5

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 " "))
Mark
  • 51
  • 1
  • 1
  • Nice, but how not to insert space when line below consisted only white spaces or was empty? – rofrol Aug 06 '21 at 17:04
4

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.

Ralph
  • 5,154
  • 1
  • 21
  • 19
3

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.

Community
  • 1
  • 1
2

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)!!
nishidh41
  • 31
  • 4
2

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:

  1. C-%
  2. Query: input C-q C-j Enter
  3. Replace: Enter
  4. Run the replacement. Enter

Done.

user5698801
  • 576
  • 4
  • 10
1

Two ways come to mind:

  1. 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 length C-x-f 1000.

  2. 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.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

"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)
CodyChan
  • 1,776
  • 22
  • 35
0

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)))
Marduk
  • 982
  • 8
  • 12
0

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)))
Paul
  • 442
  • 4
  • 9
0

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)
rofrol
  • 14,438
  • 7
  • 79
  • 77