ChatGPT解决这个技术问题 Extra ChatGPT

在 Emacs 中一次编辑多行

我相信 TextMate 有一种模式,如果您开始输入,则会在您选择的所有行上输入相同的内容。 Emacs中是否有类似的东西?

我猜有一种矩形可以帮助我,但我不确定如何......

如果您为此重新阅读 help notes,您会发现,由于它最初是发布的,它已被 multiple-cursors.el 取代
是的,@phils,multiple-cursors.el 很棒。

D
Dave Smith

就这么简单:C-x r t

这里有一些例子:http://ergoemacs.org/emacs/emacs_string-rectangle_ascii-art.html


这很棒,但对于完全不熟悉emacs的人来说不是很有用,你能帮我破译吗?
@cone 您应该阅读内置的 emacs 教程。它解释了如何阅读象形文字;)。要在 emacs 中升级教程,请在按住 ctrl 键的同时按“h”。然后,点击“t”(用于“教程”)。
我已将 C-x 映射到剪切(cua 模式),所以这不是一个好主意。
也许这是您将 C-x 映射到 cut 的想法,这不好:)
@cone 通过使用 C-<SPC> 和后来的 C-g 设置标记,我能够使用它来制作多行文本的列表。然后我做了 C-x r t,在迷你缓冲区中输入 - (破折号和一个空格),然后 <RET>。瞧!创建了一个列表。
P
Peter Mortensen

您绝对需要尝试安装 multiple cursors

它在 MarmaladeMELPA 中,所以只需:

M-x package-install multiple-cursors

这是一个很好的解决方案。非常容易使用。要通过点击添加多项选择(包括我在内的丢失人员的标准),请将 (global-unset-key (kbd "M-<down-mouse-1>")) (global-set-key (kbd "M-<mouse-1>") 'mc/add-cursor-on-click) 添加到您的配置中。
在 2021 年,我认为这是大多数人所期望的解决方案。
它甚至与 swiper 集成,使用“C-7”作为默认键绑定。 xenodium.com/emacs-swiper-and-multiple-cursors
s
smonff

其中一种解决方案是使用 CUA 模式。使用 M-x cua-mode 激活 cua 模式,选择矩形开始:首先按 C-Enter,然后使用标准移动命令移动光标以进行选择,现在随时按 enter 将使光标循环穿过矩形的角,使您可以在前面添加或附加文本选择。


谢谢,博斯科姆。我用的是21.3.1版本,所以好像没有安装这个模式。我可能不会安装它,因为我发现我喜欢 Cx r t。
谢谢!我只是认为 cua 模式是为那些不想学习 emacs 复制粘贴绑定的人准备的:P 这太棒了!
如果您使用 cua-selection-mode 而不是 cua-mode,您将不会得到不需要的剪切/复制/粘贴绑定。不过,您获得一些您可能不想要的其他功能(最值得注意的是,编辑替换区域的命令)。
p
paweloque

您可以使用以下命令(和键)来完成此操作:

open-rectangle (Cx, r, o) 添加空格

kill-rectangle (Cx, r, k) 删除

clear-rectangle (Cx, r, c) 替换为空格

Mx string-insert-rectangle 填充指定文本

以下是这些功能的完整说明:http://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html


3
3 revs, 2 users 65%

对于那些想要在更复杂的场景中执行此操作并且想要在不安装新模块的情况下执行此操作的人,请继续阅读。 (这在 Emacs 中是可能的,无需安装 MarkMultiple,尽管我个人使用并喜欢 MarkMultiple。)

我最近不得不将 SQL 查询输出到文件,然后将其格式化为“MYSQL INSERT”查询。以下是 Emacs 如何让我的生活变得轻松......

该文件如下所示:

1   I am a random text
2    I am not
3    G, you've gone mad
4    Click on this link
5   Transfer in progress (we've started the transfer process)
6    But transfer happened yesterday
7    No you are
8    Oh please! This is getting too much!
9    I love Emacs
10    I can't be bothered with    this any more
11    it's time to raise the bar
12    show me    how to expand my territory

我想让它看起来像:

(1,   ,'I am a random text'),
(2,   ,'I am not'),
(3,   ,'G, youve gone mad'),
(4,   ,'Click on this link'),
(5,   ,'Transfer in progress (weve started the transfer process)'),
(6,   ,'But transfer happened yesterday'),
(7,   ,'No you are'),
(8,   ,'Oh please! this is getting too much!'),
(9,   ,'I love Emacs'),
(10,  ,'I cant be bothered with this any more'),
(11,  ,'its time to raise the bar'),
(12,  ,'show me how to expand my territory'),

将光标放在第一行 按 Cx ( 开始录制宏(此时,您的所有键输入都被录制,因此请仔细按照说明进行操作) 按 Ca 转到行首 键入 "(" 后跟 Mf 以向前移动一个单词,然后键入 "," Cn 转到下一行,然后按 Cx ) 结束宏 Cu 11 Cx e 重复宏 n(在本例中为 11)次

尤里卡!到目前为止,如果您没有失败,您将得到如下所示的内容:

(1,   I am a random text
(2,   I am not
(3,   G, youve gone mad
(4,   Click on this link
(5,   Transfer in progress (weve started the transfer process)
(6,   But transfer happened yesterday
(7,   No you are
(8,   Oh please! this is getting too much!
(9,   I love Emacs
(10,  I cant be bothered with this any more
(11,  its time to raise the bar
(12,  show me how to expand my territory

在这一点上,我将让你弄清楚剩下的事情。但是,在我走之前,我想提一下,有很多方法可以实现这种事情。这只是其中一种方式,它恰好是我最喜欢的方式。


F3 还开始录制宏(至少在我的 emacs 中,默认情况下)并且 F4 停止录制。在 F4 上的后续点击播放宏。这比 C-x ( 等更容易记住。
单引号是如何通过此过程消失并导致文本退化的?至少应该在此答案中以某种方式解决输出中转义单引号的问题。
s
starblue

矩形用于简单的东西,例如删除相邻行中相同数量的空格。

否则键盘宏是要走的路。


A
Amjith

我相信您正在寻找 boskom 建议的 cua 模式。 http://www.vimeo.com/1168225?pg=embed&sec=1168225 此截屏视频可能会让您了解如何使用它。


谢谢你的链接!一张图片说一千个字:)。
g
gdelfino

上面显示的答案用于在列中插入文本。 TextMate 的“Edit Each Line in Selection”在每行中插入相同的文本,而不管每行的长度如何。我现在正在学习 Lisp,所以作为练习,我编写了一个函数来做到这一点:

(defun append-to-lines (text-to-be-inserted)
  ;;Appends text to each line in region
  (interactive "sEnter text to append: ")
  (save-excursion
    (let (point-ln mark-ln initial-ln final-ln count)
      (barf-if-buffer-read-only)
      (setq point-ln (line-number-at-pos))
      (exchange-point-and-mark)
      (setq mark-ln (line-number-at-pos))
      (if (< point-ln mark-ln)
          (progn (setq initial-ln point-ln final-ln mark-ln)
                 (exchange-point-and-mark))
        (setq initial-ln mark-ln final-ln point-ln))
      (setq count initial-ln)
      (while (<= count final-ln)
        (progn (move-end-of-line 1)
               (insert text-to-be-inserted)
               (next-line)
               (setq count (1+ count))))
      (message "From line %d to line %d." initial-ln final-ln ))))

您首先选择包含您想要影响的所有行,然后使用 Mx append-to-lines 运行该函数。


我还没有测试过这个,但很好的工作。我对“将相同的文本添加到同一列”方法更感兴趣,但我相信这也会派上用场。
L
LordMsz

一步一步,如何用矩形更改多行的前缀:

CTRL-x,然后按 SPACE。这将启动“矩形模式”。
现在移动光标以选择您想要的区域 - 例如多行的第一列。

ALT-x 并输入 string-rectangle
这将启动“字符串矩形'action'”,您可以在其中键入每行的操作。
(这是@allyourcode 答案中的C-x r t)。
所以输入-。并点击enter

你会在每一行得到一个“破折号”。

您可以对选择进行其他操作,例如删除等 - 请参阅@allyourcode 答案中的链接。

我没有从这里的其他人那里理解这一点,也不是从文档中理解的,抱歉 :) 我希望有人会觉得它有用。