[ACCEPTED]-By Emacs, how to join two lines into one?-editor
Place point anywhere on the last line of the 4 group of lines that need joining and call 3
repeatedly until all the lines are merged.
Note: It 2 leaves one space between all of the now 1 joined lines.
M-x join-line will join two lines. Just bind it to a 1 convenient keystroke.
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
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
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.)
Just replace newlines with nothing.
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 " "))
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.
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.
The most simplest way ever:
- Select paragraph/lines by
- Witness the Emagics (Emacs Magic)!!
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:
- Query: input
- Run the replacement.
Two ways come to mind:
Once you think of 5 it, the most obvious (or at least easiest 4 to remember) way is to use
M-q format-paragraphwith a long 3 line length
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.
"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)
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)))
A basic join of 2 lines:
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)))
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
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