通过Emacs,如何将两行合二为一?

发布于 2024-07-25 18:53:26 字数 161 浏览 4 评论 0原文

我是 Emacs 新手。 我用谷歌搜索过这个,但没有好的答案。 其中之一是 Ctrl-n Ctrl-a 退格键 这有效但很愚蠢。 有没有一种快速而简单的方法将一行行连接成一行?

实际上,我现在可以使用 Esc-q 自动填充段落,但是如何在不撤消的情况下恢复它呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

稚然 2024-08-01 18:53:26

将点放置在需要连接的行组的最后行上的任意位置,并

M-^

重复调用,直到合并所有行。

注意:它在所有现在连接的行之间留下一个空格。

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.

许一世地老天荒 2024-08-01 18:53:26

Mx join-line 将连接两条线。 只需将其绑定到方便的击键即可。

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

念三年u 2024-08-01 18:53:26

多个光标与 M-^ 结合会将所有选定的行折叠成一行,并带有所有无关的白色 -空间被移除。

例如,要选择整个缓冲区,调用多游标模式,折叠成一行,然后禁用多游标模式:

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

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
つ低調成傷 2024-08-01 18:53:26

Emacs 中“join”的常规名称是“fill”。 是的,您可以加入
行带有M-^——这很方便——但更一般地说,你会
想要加入 n 行。 为此,请参阅 fill* 命令,例如
fill-regionfill-paragraph 等。

请参阅了解更多信息
选择可以填充的东西。

此外,您可以通过先选择这些行来使用 M-^ 连接多行。 (请注意,通用论证不适用于此。)

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

凑诗 2024-08-01 18:53:26

只需将换行符替换为空即可。

Just replace newlines with nothing.

梦断已成空 2024-08-01 18:53:26

我喜欢 Sublime text 使用 Command J 连接行的方式,所以我这样做:

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

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 " "))
我家小可爱 2024-08-01 18:53:26

您可以为此定义一个新命令,在使用 Esc-q 命令之前临时调整填充宽度:

;; -- define a new command to join multiple lines together --
(defun join-lines () (interactive)
 (setq fill-column 100000)
 (fill-paragraph nil)
 (setq fill-column 78)
)

显然,只有当您的段落少于 100000 个字符时,这才有效。

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.

童话 2024-08-01 18:53:26

我使用以下函数并将其绑定到“MJ”。

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

如果您希望保持光标位置,可以使用 保存游览

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.

浮世清欢 2024-08-01 18:53:26

最简单的方法:

  1. 通过MhC-SPC选择段落/行
  2. Mq
  3. 见证Emagics(Emacs Magic)!!

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)!!
傾旎 2024-08-01 18:53:26

由于 join-line 会在两行之间留一个空格,因此它只支持连接两行。 如果需要连接大量行且不留一个空格,可以使用“搜索-替换”模式来解决,如下:

  1. C-%
  2. 查询:输入Cq Cj Enter
  3. 替换: Enter
  4. 运行替换。 输入

完成。

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.

世俗缘 2024-08-01 18:53:26

我想到了两种方法:

  1. 一旦您想到它,最明显(或至少最容易记住)的方法是使用 Mq format-paragraph 和长行长度 Cxf 1000.

  2. 还有一个内置工具M-^ join-line。 更有用的是,如果您选择一个区域,那么它会将它们全部组合成一行。

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.

沉默的熊 2024-08-01 18:53:26

“我怎样才能让它在不撤消的情况下恢复?”:

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

"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)
无人接听 2024-08-01 18:53:26

来自 EmacsWiki:取消填充段落

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

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)))
坏尐絯℡ 2024-08-01 18:53:26

两行的基本连接:

(delete-indentation)

我喜欢将下面的行连接到当前行而不移动光标:

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

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)))
逆蝶 2024-08-01 18:53:26

这个行为就像 vscode 中一样。 因此,仅当连接线包含空格以外的内容时,它才会添加空格。 我将它绑定到alt+shift+j

基于 crux-top-join-line 的较短版本:

(global-set-key (kbd "M-J") (lambda () (interactive) (delete-indentation 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文