Answers:
我用
C-a C-SPACE C-n M-w C-y
分解为
C-a
:将光标移到行首C-SPACE
:开始选择(“设置标记”)C-n
:将光标移到下一行M-w
:复制区域C-y
:粘贴(“扬”)之前所提
C-a C-k C-k C-y C-y
等于同一件事(TMTOWTDI)
C-a
:将光标移到行首C-k
:切断(“杀死”)线C-k
:剪切换行符C-y
:粘贴(“扬”)(我们回到第一个方框)C-y
:再次粘贴(现在我们有该行的两个副本)与C-d
您的编辑器相比,它们都令人尴尬地冗长,但是在Emacs中始终有一个自定义项。C-d
是delete-char
默认绑定的,那又如何C-c C-d
呢?只需将以下内容添加到您的.emacs
:
(global-set-key "\C-c\C-d" "\C-a\C- \C-n\M-w\C-y")
(@Nathan的elisp版本可能更可取,因为如果更改任何键绑定,它都不会中断。)
当心:某些Emacs模式可能会收回C-c C-d
来做其他事情。
C-S-backspace C-y C-y
呢
除了前面的答案,您还可以定义自己的函数来复制一行。例如,将以下内容放入您的.emacs文件将使Cd复制当前行。
(defun duplicate-line()
(interactive)
(move-beginning-of-line 1)
(kill-line)
(yank)
(open-line 1)
(next-line 1)
(yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)
Symbol's function definition is void: move-beginning-of-line
Del
此功能绑定的任何想法?
Del
在保留新值的同时恢复正常的解决方案C-d
:(global-set-key (kbd "<delete>") 'delete-char)
需要在C-d
定义之后添加。
我的函数版本可以复制一条行,该行可以与undo一起很好地工作,并且不会弄乱光标的位置。这是1997年11月在gnu.emacs.sources中进行讨论的结果。
(defun duplicate-line (arg)
"Duplicate current line, leaving point in lower line."
(interactive "*p")
;; save the point for undo
(setq buffer-undo-list (cons (point) buffer-undo-list))
;; local variables for start and end of line
(let ((bol (save-excursion (beginning-of-line) (point)))
eol)
(save-excursion
;; don't use forward-line for this, because you would have
;; to check whether you are at the end of the buffer
(end-of-line)
(setq eol (point))
;; store the line and disable the recording of undo information
(let ((line (buffer-substring bol eol))
(buffer-undo-list t)
(count arg))
;; insert the line arg times
(while (> count 0)
(newline) ;; because there is no newline in 'line'
(insert line)
(setq count (1- count)))
)
;; create the undo information
(setq buffer-undo-list (cons (cons eol (point)) buffer-undo-list)))
) ; end-of-let
;; put the point in the lowest line and return
(next-line arg))
然后,您可以定义CTRL-D来调用此函数:
(global-set-key (kbd "C-d") 'duplicate-line)
crux-duplicate-current-line-or-region
对我来说效果更好,因为使用您的功能,它可以撤消行重复和最后一次操作。
相反的kill-line
(C-k
),如C-a
C-k
C-k
C-y
C-y
使用kill-whole-line
命令:
C-S-Backspace
C-y
C-y
超过的优点C-k
包括:在线上的点无关紧要(与C-k
要求在行的开始处不同)不一样,并且它还会终止换行符(同样,某些C-k
操作无效)。
这是执行此操作的另一个功能。我的版本没有碰到kill ring,光标最终移到了原来的新行上。如果该区域处于活动状态(瞬变标记模式),它将复制该区域;否则,默认情况下将复制该行。如果给定了前缀arg,它还将产生多个副本,如果给定一个负前缀arg,它将注释掉原始行(这对于在保留旧命令/语句的同时测试不同版本的命令/语句很有用)。
(defun duplicate-line-or-region (&optional n)
"Duplicate current line, or region if active.
With argument N, make N copies.
With negative N, comment out original line and use the absolute value."
(interactive "*p")
(let ((use-region (use-region-p)))
(save-excursion
(let ((text (if use-region ;Get region if active, otherwise line
(buffer-substring (region-beginning) (region-end))
(prog1 (thing-at-point 'line)
(end-of-line)
(if (< 0 (forward-line 1)) ;Go to beginning of next line, or make a new one
(newline))))))
(dotimes (i (abs (or n 1))) ;Insert N times, or once if not specified
(insert text))))
(if use-region nil ;Only if we're working with a line (not a region)
(let ((pos (- (point) (line-beginning-position)))) ;Save column
(if (> 0 n) ;Comment out original with negative arg
(comment-region (line-beginning-position) (line-end-position)))
(forward-line 1)
(forward-char pos)))))
我将其绑定到C-c d
:
(global-set-key [?\C-c ?d] 'duplicate-line-or-region)
永远不要通过模式或其他任何方式重新分配它,因为C-c
后面紧跟着一个(未修改的)字母供用户绑定。
C-c d
,出现错误command-execute: Wrong type argument: commandp, duplicate-line-or-region
。知道发生了什么吗?我在Windows上使用Emacs 25.1.1
内森(Nathan)在.emacs文件中的添加方式是可行的,但是可以通过替换来稍微简化它
(open-line 1)
(next-line 1)
与
(newline)
屈服
(defun duplicate-line()
(interactive)
(move-beginning-of-line 1)
(kill-line)
(yank)
(newline)
(yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)
我不太记得行复制在其他任何地方的工作方式,但是作为前SciTE用户,我喜欢SciTE-way的一件事:它不会碰到光标位置!因此,以上所有方法对我来说都不足够,这是我的嬉皮版本:
(defun duplicate-line ()
"Clone line at cursor, leaving the latter intact."
(interactive)
(save-excursion
(let ((kill-read-only-ok t) deactivate-mark)
(toggle-read-only 1)
(kill-whole-line)
(toggle-read-only 0)
(yank))))
请注意,实际上没有任何东西在进程中被杀死,不会留下任何痕迹和当前选择。
顺便说一句,为什么你们会喜欢在这种不错的“整洁的杀人”(CS退格键)(CS退格键)周围晃动光标?
您可能想要在.emacs中包含的内容是
(setq kill-whole-line t)
基本上,每当您调用kill-line(即通过Ck)时,都会杀死整行以及换行符。然后,无需额外的代码,您只需执行Ca Ck Cy Cy即可复制该行。它分解为
C-a go to beginning of line
C-k kill-line (i.e. cut the line into clipboard)
C-y yank (i.e. paste); the first time you get the killed line back;
second time gives the duplicated line.
但是,如果您经常使用它,那么专用的键绑定也许是一个更好的主意,但是仅使用Ca Ck Cy Cy的好处是您可以在其他位置复制该行,而不仅仅是在当前行下方。
我copy-from-above-command
绑定了一个键并使用它。它是XEmacs附带的,但是我不知道GNU Emacs。
“从上方复制命令”是一个交互式编译的Lisp函数
,从“ /usr/share/xemacs/21.4.15/lisp/misc.elc”加载(从上方复制命令和可选的ARG)文档:从上一行非空白行复制字符。复制ARG字符,但不要超过该行的末尾。如果未提供任何参数,请复制该行的其余部分。复制的字符将插入到缓冲区之前。
GNU Emacs 23.2.1 (amd64-portbld-freebsd8.1) of 2010-11-14 on [host clipped]
。
默认设置对此非常可怕。但是,您可以扩展Emacs使其像SlickEdit和TextMate一样工作,也就是说,当没有选择任何文本时,复制/剪切当前行:
(transient-mark-mode t)
(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, copy a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Copied line")
(list (line-beginning-position)
(line-beginning-position 2)))))
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
将以上内容放在中.emacs
。然后,复制一行M-w
。要删除一行,C-w
。要复制一行,C-a M-w C-y C-y C-y ...
。
'我写了自己的版本duplicate-line
,因为我不想弄死杀手。
(defun jr-duplicate-line ()
"EASY"
(interactive)
(save-excursion
(let ((line-text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(move-end-of-line 1)
(newline)
(insert line-text))))
(global-set-key "\C-cd" 'jr-duplicate-line)
我喜欢FraGGod的版本,除了两件事:(1)它不检查缓冲区是否已经是只读的(interactive "*")
;(2)如果最后一行为空,则它在缓冲区的最后一行失败(如您在这种情况下将无法终止该行),从而使缓冲区保持只读状态。
我进行了以下更改以解决该问题:
(defun duplicate-line ()
"Clone line at cursor, leaving the latter intact."
(interactive "*")
(save-excursion
;; The last line of the buffer cannot be killed
;; if it is empty. Instead, simply add a new line.
(if (and (eobp) (bolp))
(newline)
;; Otherwise kill the whole line, and yank it back.
(let ((kill-read-only-ok t)
deactivate-mark)
(toggle-read-only 1)
(kill-whole-line)
(toggle-read-only 0)
(yank)))))
使用最新的emacs,您可以在行中的任何位置使用Mw进行复制。这样就变成了:
M-w C-a RET C-y
M-w
绑定到easy-kill
。检查当您做得到的东西C-h c M-w
无论如何,我看到了非常复杂的解决方案...
(defun duplicate-line ()
"Duplicate current line"
(interactive)
(kill-whole-line)
(yank)
(yank))
(global-set-key (kbd "C-x M-d") 'duplicate-line)
ctrl- k,ctrl- k(到新位置的位置)ctrl-y
如果您不是从行首开始,请添加ctrl- a。而第二个ctrl- k就是抓住换行符。如果只需要文本,可以将其删除。
在没有活动区域的情况下进行交互式调用时,请复制(Mw)一行:
(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, COPY a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Copied line")
(list (line-beginning-position)
(line-beginning-position 2)))))
在没有活动区域的情况下进行交互式调用时,请杀死(Cw)一行。
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, KILL a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Killed line")
(list (line-beginning-position)
(line-beginning-position 2)))))
另外,在相关说明中:
(defun move-line-up ()
"Move up the current line."
(interactive)
(transpose-lines 1)
(forward-line -2)
(indent-according-to-mode))
(defun move-line-down ()
"Move down the current line."
(interactive)
(forward-line 1)
(transpose-lines 1)
(forward-line -1)
(indent-according-to-mode))
(global-set-key [(meta shift up)] 'move-line-up)
(global-set-key [(meta shift down)] 'move-line-down)
我根据自己的喜好写一个。
(defun duplicate-line ()
"Duplicate current line."
(interactive)
(let ((text (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(cur-col (current-column)))
(end-of-line) (insert "\n" text)
(beginning-of-line) (right-char cur-col)))
(global-set-key (kbd "C-c d l") 'duplicate-line)
但是我发现当当前行包含多字节字符(例如CJK字符)时,这将有一些问题。如果遇到此问题,请尝试以下方法:
(defun duplicate-line ()
"Duplicate current line."
(interactive)
(let* ((text (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(cur-col (length (buffer-substring-no-properties (point-at-bol) (point)))))
(end-of-line) (insert "\n" text)
(beginning-of-line) (right-char cur-col)))
(global-set-key (kbd "C-c d l") 'duplicate-line)
在按行或区域复制,然后按预期保留点和/或活动区域方面,此功能应与JetBrains的实现相匹配:
只是交互形式的包装:
(defun wrx/duplicate-line-or-region (beg end)
"Implements functionality of JetBrains' `Command-d' shortcut for `duplicate-line'.
BEG & END correspond point & mark, smaller first
`use-region-p' explained:
http://emacs.stackexchange.com/questions/12334/elisp-for-applying-command-to-only-the-selected-region#answer-12335"
(interactive "r")
(if (use-region-p)
(wrx/duplicate-region-in-buffer beg end)
(wrx/duplicate-line-in-buffer)))
哪个叫这个
(defun wrx/duplicate-region-in-buffer (beg end)
"copy and duplicate context of current active region
|------------------------+----------------------------|
| before | after |
|------------------------+----------------------------|
| first <MARK>line here | first line here |
| second item<POINT> now | second item<MARK>line here |
| | second item<POINT> now |
|------------------------+----------------------------|
TODO: Acts funky when point < mark"
(set-mark-command nil)
(insert (buffer-substring beg end))
(setq deactivate-mark nil))
或这个
(defun wrx/duplicate-line-in-buffer ()
"Duplicate current line, maintaining column position.
|--------------------------+--------------------------|
| before | after |
|--------------------------+--------------------------|
| lorem ipsum<POINT> dolor | lorem ipsum dolor |
| | lorem ipsum<POINT> dolor |
|--------------------------+--------------------------|
TODO: Save history for `Cmd-Z'
Context:
http://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs#answer-551053"
(setq columns-over (current-column))
(save-excursion
(kill-whole-line)
(yank)
(yank))
(let (v)
(dotimes (n columns-over v)
(right-char)
(setq v (cons n v))))
(next-line))
然后我将其绑定到meta + shift + d
(global-set-key (kbd "M-D") 'wrx/duplicate-line-or-region)
具有前缀参数,什么是(我希望)直观的行为:
(defun duplicate-line (&optional arg)
"Duplicate it. With prefix ARG, duplicate ARG times."
(interactive "p")
(next-line
(save-excursion
(let ((beg (line-beginning-position))
(end (line-end-position)))
(copy-region-as-kill beg end)
(dotimes (num arg arg)
(end-of-line) (newline)
(yank))))))
光标将停留在最后一行。或者,您可能想要指定一个前缀以一次复制接下来的几行:
(defun duplicate-line (&optional arg)
"Duplicate it. With prefix ARG, duplicate ARG times."
(interactive "p")
(save-excursion
(let ((beg (line-beginning-position))
(end
(progn (forward-line (1- arg)) (line-end-position))))
(copy-region-as-kill beg end)
(end-of-line) (newline)
(yank)))
(next-line arg))
我发现自己经常使用包装函数来切换prefix参数的行为。
和一个绑定:
(global-set-key (kbd "C-S-d") 'duplicate-line)
;; http://www.emacswiki.org/emacs/WholeLineOrRegion#toc2
;; cut, copy, yank
(defadvice kill-ring-save (around slick-copy activate)
"When called interactively with no active region, copy a single line instead."
(if (or (use-region-p) (not (called-interactively-p)))
ad-do-it
(kill-new (buffer-substring (line-beginning-position)
(line-beginning-position 2))
nil '(yank-line))
(message "Copied line")))
(defadvice kill-region (around slick-copy activate)
"When called interactively with no active region, kill a single line instead."
(if (or (use-region-p) (not (called-interactively-p)))
ad-do-it
(kill-new (filter-buffer-substring (line-beginning-position)
(line-beginning-position 2) t)
nil '(yank-line))))
(defun yank-line (string)
"Insert STRING above the current line."
(beginning-of-line)
(unless (= (elt string (1- (length string))) ?\n)
(save-excursion (insert "\n")))
(insert string))
(global-set-key (kbd "<f2>") 'kill-region) ; cut.
(global-set-key (kbd "<f3>") 'kill-ring-save) ; copy.
(global-set-key (kbd "<f4>") 'yank) ; paste.
将上面的elisp添加到您的init.el中,您现在可以剪切/复制整行功能,然后可以按F3 F4复制一条线。
最简单的方法是克里斯·康威(Chris Conway)的方法。
C-a C-SPACE C-n M-w C-y
这是EMACS规定的默认方式。我认为,最好使用该标准。我始终对在EMACS中自定义自己的键绑定保持谨慎。EMACS已经足够强大,我认为我们应该尽力适应其自身的键绑定。
虽然有点冗长,但是当您习惯了它之后,您可以快速执行并发现这很有趣!
这是复制当前行的功能。使用前缀参数,它将多次复制该行。例如,C-3 C-S-o
将当前行重复三遍。不改变杀死戒指。
(defun duplicate-lines (arg)
(interactive "P")
(let* ((arg (if arg arg 1))
(beg (save-excursion (beginning-of-line) (point)))
(end (save-excursion (end-of-line) (point)))
(line (buffer-substring-no-properties beg end)))
(save-excursion
(end-of-line)
(open-line arg)
(setq num 0)
(while (< num arg)
(setq num (1+ num))
(forward-line 1)
(insert-string line))
)))
(global-set-key (kbd "C-S-o") 'duplicate-lines)