我相信 TextMate 有一种模式,如果您开始输入,则会在您选择的所有行上输入相同的内容。 Emacs中是否有类似的东西?
我猜有一种矩形可以帮助我,但我不确定如何......
您绝对需要尝试安装 multiple cursors。
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)
添加到您的配置中。
其中一种解决方案是使用 CUA 模式。使用 M-x cua-mode
激活 cua 模式,选择矩形开始:首先按 C-Enter
,然后使用标准移动命令移动光标以进行选择,现在随时按 enter 将使光标循环穿过矩形的角,使您可以在前面添加或附加文本选择。
cua-selection-mode
而不是 cua-mode
,您将不会得到不需要的剪切/复制/粘贴绑定。不过,您将获得一些您可能不想要的其他功能(最值得注意的是,编辑替换区域的命令)。
您可以使用以下命令(和键)来完成此操作:
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
对于那些想要在更复杂的场景中执行此操作并且想要在不安装新模块的情况下执行此操作的人,请继续阅读。 (这在 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 (
等更容易记住。
矩形用于简单的东西,例如删除相邻行中相同数量的空格。
否则键盘宏是要走的路。
我相信您正在寻找 boskom 建议的 cua 模式。 http://www.vimeo.com/1168225?pg=embed&sec=1168225 此截屏视频可能会让您了解如何使用它。
上面显示的答案用于在列中插入文本。 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 运行该函数。
一步一步,如何用矩形更改多行的前缀:
按 CTRL-x
,然后按 SPACE
。这将启动“矩形模式”。
现在移动光标以选择您想要的区域 - 例如多行的第一列。
按 ALT-x
并输入 string-rectangle
。
这将启动“字符串矩形'action'”,您可以在其中键入每行的操作。
(这是@allyourcode 答案中的C-x r t
)。
所以输入-
。并点击enter
。
你会在每一行得到一个“破折号”。
您可以对选择进行其他操作,例如删除等 - 请参阅@allyourcode 答案中的链接。
我没有从这里的其他人那里理解这一点,也不是从文档中理解的,抱歉 :) 我希望有人会觉得它有用。
不定期副业成功案例分享
C-x
映射到剪切(cua 模式),所以这不是一个好主意。C-x
映射到 cut 的想法,这不好:)C-<SPC>
和后来的C-g
设置标记,我能够使用它来制作多行文本的列表。然后我做了C-x r t
,在迷你缓冲区中输入-
(破折号和一个空格),然后 <RET>。瞧!创建了一个列表。