[ACCEPTED]-By Emacs, how to join two lines into one?-editor

Accepted answer
Score: 195

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

M-^

repeatedly until all the lines are merged.

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

Score: 36

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

Score: 10

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

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

C-x h
M-x mc/edit-lines
M-^
C-g
Score: 10

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

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

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

Score: 9

Just replace newlines with nothing.

0

Score: 5

I like the way Sublime text Join line with 1 Command J so I do it this way:

(defun join-lines (arg)
  (interactive "p")
  (end-of-line)
  (delete-char 1)
  (delete-horizontal-space)
  (insert " "))
Score: 4

You could define a new command for this, temporarily 3 adjusting the fill width before using the 2 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 1 your paragraph has less than 100000 characters.

Score: 3

I use the following function and bind it 2 to 'M-J'.

(defun concat-lines ()
  (interactive)
  (next-line)
  (join-line)
  (delete-horizontal-space))

If you prefer to keep your cursor 1 position, you can use save-excursion.

Score: 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)!!

0

Score: 1

Because join-line will left one space between two 4 lines, also it only support join two lines. In 3 case of you want to join plenty of lines 2 without one space left, you can use "search-replace" mode 1 to solve, as follows:

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

Done.

Score: 1

Two ways come to mind:

  1. Once you think of 5 it, the most obvious (or at least easiest 4 to remember) way is to use M-q format-paragraph with a long 3 line length C-x-f 1000.

  2. There is also a built-in tool 2 M-^ join-line. More usefully, if you select a region 1 then it will combine them all into one line.

Score: 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)

0

Score: 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)))

0

Score: 0

A basic join of 2 lines:

(delete-indentation)

I like to line below 2 to be joined to the current without moving 1 the cursor:

("C-j" .
  (lambda (iPoint)
    "Join next line onto current line"
    (interactive "d")
    (next-line)
    (delete-indentation)
    (goto-char iPoint)))
Score: 0

This one behaves like in vscode. So it add 4 space only if join line consisted something 3 else than whitespace. And I bind it to alt+shift+j.

Shorter 2 version based on crux-top-join-line:

(global-set-key (kbd "M-J") (lambda () (interactive) (delete-indentation 1)))

Longer version based on 1 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)

More Related questions