如何在 Emacs 中复制整行?
我看到了 VIM 的同样问题,这是我我自己想知道如何使用 Emacs。 在 ReSharper 中,我使用 CTRL-D 执行此操作。 在 Emacs 中执行此操作最少需要多少命令?
I saw this same question for VIM and it has been something that I myself wanted to know how to do for Emacs. In ReSharper I use CTRL-D for this action. What is the least number of commands to perform this in Emacs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
相对于克里斯·康威选择的答案,这感觉更自然。
这样,您只需重复 Ctrl-c Ctrl-d 按键即可多次复制一行。
This feels more natural, with respect to the selected answer by Chris Conway.
This allows you to duplicate a line multiple times by simply repeating the Ctrl-c Ctrl-d key strokes.
我根据自己的喜好写一篇。
但我发现当当前行包含多字节字符(例如CJK 字符)时,这会出现一些问题。 如果您遇到此问题,请尝试以下操作:
I write one for my preference.
But I found this would have some problem when current line contains multi-byte characters (e.g. CJK characters). If you encounter this issue, try this instead:
这是复制当前行的函数。 使用前缀参数,它将多次重复该行。 例如,
C-3 CSo
会将当前行复制三遍。 不改变杀环。Here's a function for duplicating current line. With prefix arguments, it will duplicate the line multiple times. E.g.,
C-3 C-S-o
will duplicate the current line three times. Doesn't change kill ring.如果您使用的是 Spacemacs,则只需使用
duplicate-line-or-region
即可,绑定到:If you're using Spacemacs, you can simply use
duplicate-line-or-region
, bound to:Melpa 上有一个名为“move-dup”的软件包可以帮助您。
免责声明:我是该包的作者。
There's a package called 'move-dup' on Melpa that can help you with that.
Disclaimer: I'm the author of that package.
ctrl-k,ctrl-k,(定位到新位置)ctrl -y
如果您不是从行首开始,请添加 ctrl-a。 第二个 ctrl-k 是抓取换行符。 如果您只需要文本,可以将其删除。
ctrl-k, ctrl-k, (position to new location) ctrl-y
Add a ctrl-a if you're not starting at the beginning of the line. And the 2nd ctrl-k is to grab the newline character. It can be removed if you just want the text.
正如其他答案中提到的,将击键绑定到 lisp 代码比将它们绑定到另一个击键更好。 根据 @mw 的回答,代码复制该行并将标记移动到新行的末尾。 此修改将标记位置保持在新行的同一列:
As mentioned in other answers, binding key strokes to lisp code is a better idea than binding them to another key strokes. With @mw's answer, code duplicates the line and moves the mark to end of new line. This modification keeps the mark position at same column on the new line:
我无法相信所有这些复杂的解决方案。 这是两次击键:
运行命令kill-whole-lineC-/
运行命令undo因此
; C-/
“复制”整行(终止和撤消)。当然,您可以将其与数字和负参数结合起来,以向前或向后杀死多行。
I cannot believe all these complicated solutions. This is two keystrokes:
<C-S-backspace>
runs the command kill-whole-lineC-/
runs the command undoSo
<C-S-backspace> C-/
to "copy" a whole line (kill and undo).You can, of course, combine this with numeric and negative args to kill multiple lines either forward or backward.
我喜欢 FraGGod 的版本,除了两件事:(1) 它不会检查缓冲区是否已经是只读的
(interactive "*")
,以及 (2) 它在最后一次失败如果最后一行为空(因为在这种情况下无法终止该行),则缓冲区的行,使缓冲区保持只读状态。我进行了以下更改来解决该问题:
I liked FraGGod's version, except for two things: (1) It doesn't check whether the buffer is already read-only with
(interactive "*")
, and (2) it fails on the last line of the buffer if that last line is empty (as you cannot kill the line in that case), leaving your buffer read-only.I made the following changes to resolve that:
使用最近的 emacs,您可以在行中的任何位置使用 Mw 来复制它。 所以就变成了:
With recent emacs, you can use M-w anywhere in the line to copy it. So it becomes:
无论如何,我看到了非常复杂的解决方案......
I saw very complex solutions, anyway...
当没有活动区域交互调用时,改为 COPY (Mw) 单行:
当没有活动区域交互调用时,改为 KILL (Cw) 单行。
另外,相关说明:
When called interactively with no active region, COPY (M-w) a single line instead :
When called interactively with no active region, KILL (C-w) a single line instead.
Also, on a related note:
@[Kevin Conner]:据我所知,非常接近。 唯一需要考虑的另一件事是打开
kill-whole-line
以在 Ck 中包含换行符。@[Kevin Conner]: Pretty close, so far as I know. The only other thing to consider is turning on
kill-whole-line
to include the newline in the C-k.我不太记得行复制在其他地方是如何工作的,但作为一名前 SciTE 用户,我喜欢 SciTE-way 的一件事:它不会触及光标位置!
所以上面的所有食谱对我来说都不够好,这是我的嬉皮版本:
请注意,在过程中没有任何东西被真正杀死,留下标记和当前选择完好无损。
顺便说一句,为什么你们这么喜欢在有这个漂亮的干净的杀整行东西(CS-退格键)时晃动光标?
I don't quite remember how line duplication works anywhere else, but as a former SciTE user I liked one thing about SciTE-way: it doesn't touch the cursor position!
So all the recipies above weren't good enough for me, here's my hippie-version:
Note that nothing gets actually killed in process, leaving marks and current selection intact.
BTW, why you guys so fond of jerking cursor around when there's this nice'n'clean kill-whole-line thingy (C-S-backspace)?
从 melpa 安装重复的东西:
Mx package-install RET 重复的东西
并将此键绑定添加到 初始化文件 :
(global-set-key (kbd "Mc") 'duplicate-thing)
install duplicate-thing from melpa:
M-x package-install RET duplicate-thing
and add this keybinding to init file :
(global-set-key (kbd "M-c") 'duplicate-thing)
而不是
Ca
中的kill-line
(Ck
)Ck
Ck
Cy
Cy
使用kill-whole-line
命令:相对于
Ck
的优点包括,点在线上的位置并不重要(与Ck
不同) >Ck 需要位于行的开头)并且它还会杀死换行符(同样是Ck
不做的事情)。Instead of
kill-line
(C-k
) as inC-a
C-k
C-k
C-y
C-y
use thekill-whole-line
command:The advantages over
C-k
include that it does not matter where point is on the line (unlikeC-k
which requires being at start of the line) and it also kills the newline (again somethingC-k
does not do).我使用
它分解为
Ca
:将光标移动到行首C-SPACE
:开始选择(“设置标记”)Cn
:将光标移至下一行Mw
:复制区域Cy
:粘贴(“yank”)上述
内容相同 (TMTOWTDI)
Ca
:将光标移动到行首Ck
:剪切(“kill”)该行Ck
:剪切换行Cy
:粘贴(“yank”) (我们回到了第一个位置)Cy
:再次粘贴(现在我们有该行的两个副本)与编辑器中的
Cd
相比,这些都冗长得令人尴尬,但在 Emacs 中总是有自定义。Cd
默认绑定到delete-char
,那么Cc Cd
又如何呢? 只需将以下内容添加到您的.emacs
中即可:(@Nathan 的 elisp 版本可能更可取,因为如果更改任何键绑定,它也不会中断。)
请注意:某些 Emacs 模式可能会回收
抄送 Cd
去做其他事情。I use
which breaks down to
C-a
: move cursor to start of lineC-SPACE
: begin a selection ("set mark")C-n
: move cursor to next lineM-w
: copy regionC-y
: paste ("yank")The aforementioned
amounts to the same thing (TMTOWTDI)
C-a
: move cursor to start of lineC-k
: cut ("kill") the lineC-k
: cut the newlineC-y
: paste ("yank") (we're back at square one)C-y
: paste again (now we've got two copies of the line)These are both embarrassingly verbose compared to
C-d
in your editor, but in Emacs there's always a customization.C-d
is bound todelete-char
by default, so how aboutC-c C-d
? Just add the following to your.emacs
:(@Nathan's elisp version is probably preferable, because it won't break if any of the key bindings are changed.)
Beware: some Emacs modes may reclaim
C-c C-d
to do something else.除了前面的答案之外,您还可以定义自己的函数来复制行。 例如,将以下内容放入 .emacs 文件中将使 Cd 复制当前行。
In addition to the previous answers you can also define your own function to duplicate a line. For example, putting the following in your .emacs file will make C-d duplicate the current line.
将光标放在行上,如果不在开头执行 CTRL-A,则:
CTRL-K
CTRL-K
CTRL-Y
CTRL-Y
Place cursor on line, if not at beginning do a CTRL-A, then:
CTRL-K
CTRL-K
CTRL-Y
CTRL-Y
我的复制行函数版本可以很好地进行撤消操作,并且不会扰乱光标位置。 这是 1997 年 11 月在 gnu.emacs.sources 中的讨论。
然后你可以定义 CTRL-D 来调用这个函数:
My version of a function to duplicate a line that works nice with undo and doesn't mess with the cursor position. It was the result of a discussion in gnu.emacs.sources from November 1997.
Then you can define CTRL-D to call this function:
这是执行此操作的另一个函数。 我的版本没有触及终止环,并且光标最终位于原始版本上的新行上。 如果它处于活动状态(瞬态标记模式),它将复制该区域,否则默认复制该行。 如果给定前缀 arg,它还会制作多个副本,如果给定负前缀 arg,它还会注释掉原始行(这对于在保留旧版本的同时测试不同版本的命令/语句非常有用)。
我将其绑定到
Cc d
:这永远不应该由模式或任何东西重新分配,因为
Cc
后跟一个(未修改的)字母是为用户绑定保留的。Here's yet another function for doing this. My version doesn't touch the kill ring, and the cursor ends up on the new line where it was on the original. It will duplicate the region if it's active (transient mark mode), or default to duplicating the line otherwise. It will also make multiple copies if given a prefix arg, and comment out the original line if given a negative prefix arg (this is useful for testing a different version of a command/statement while keeping the old one).
I have it bound to
C-c d
:This should never be re-assigned by a mode or anything because
C-c
followed by a single (unmodified) letter is reserved for user bindings.Nathan 添加到 .emacs 文件中是可行的方法,但可以通过替换
为
Yieling来稍微简化
Nathan's addition to your .emacs file is the way to go but it could be simplified slightly by replacing
with
yielding
默认值对于这个来说是可怕的。 但是,您可以扩展 Emacs 使其像 SlickEdit 和 TextMate 一样工作,即在未选择文本时复制/剪切当前行:
将以上内容放入
.emacs
中。 然后,要复制一行,Mw
。 要删除一行,Cw
。 要复制一行,Ca Mw Cy Cy Cy ...
。The defaults are horrible for this. However, you can extend Emacs to work like SlickEdit and TextMate, that is, copy/cut the current line when no text is selected:
Place the above in
.emacs
. Then, to copy a line,M-w
. To delete a line,C-w
. To duplicate a line,C-a M-w C-y C-y C-y ...
.因为我不知道,我将以慢球开始这一轮高尔夫:
ctrl-k,y,y
because i don't know, i'll start this round of golf with a slowball:
ctrl-k, y, y
有一个名为 Avy 的包,它有命令 avy-copy-line。 当您使用该命令时,窗口中的每一行都会获得字母组合。 然后你只需输入组合即可得到该行。 这也适用于区域。 然后你只需输入两个组合即可。
在这里你可以看到界面:
There is package called Avy It has command avy-copy-line. When you use that command, every line in your window gets letter combination. Then you just have to type combination and you get that line. This also works for region. Then you just have to type two combination.
Here you can see interface:
此功能应与 JetBrains 的实现相匹配,即按行或区域复制,然后按预期保留点和/或活动区域:
只是交互式表单的包装器:
它调用此,
或此
,然后我将其绑定到meta+shift+d
This functionality should match up with JetBrains' implementation in terms of duplicating both by line or region, and then leaving the point and/ or active region as expected:
Just a wrapper to around the interactive form:
Which calls this,
Or this
And then I have this bound to meta+shift+d
您可能希望在 .emacs 中拥有的内容是,
每当您调用kill-line(即通过 Ck)时,它基本上会杀死整行加上换行符。 然后,无需额外代码,您只需执行 Ca Ck Cy Cy 即可复制该行。 它分解为
但如果您经常使用它,那么专用的键绑定可能是一个更好的主意,但仅使用 Ca Ck Cy Cy 的优点是您可以在其他地方复制该行,而不是在当前行下方。
something you might want to have in your .emacs is
Which basically kills the entire line plus the newline whenever you invoke kill-line (i.e. via C-k). Then without extra code, you can just do C-a C-k C-y C-y to duplicate the line. It breaks down to
But if you use this often then maybe a dedicated key binding might be a better idea, but the advantage of just using C-a C-k C-y C-y is you can duplicate the line elsewhere, instead of just below the current line.
我将
copy-from-above-command
绑定到一个键并使用它。 XEmacs 提供了它,但我不知道 GNU Emacs。I have
copy-from-above-command
bound to a key and use that. It's provided with XEmacs, but I don't know about GNU Emacs.'我写了我自己的
duplicate-line
版本,因为我不想搞砸杀戮圈。' I wrote my own version of
duplicate-line
, cause I don't want to screw up the killing ring.