如何在Emacs中复制整行?

如何在Emacs中复制整行?,emacs,text,editor,editing,command,Emacs,Text,Editor,Editing,Command,我看到了,这是我自己想知道如何为Emacs做的事情。在ReSharper中,我使用CTRL-D进行此操作。在Emacs中执行此操作的最少命令数是多少?因为我不知道,我将以慢球开始这轮高尔夫: ctrl-k,y,yctrl-k,ctrl-k,(定位到新位置)ctrl-y 如果不是从行首开始,请添加ctrl-a。第二个ctrl-k是获取换行符。如果您只需要文本,可以将其删除。将光标放在行上,如果不是在开始时按CTRL-a键,则: CTRL-K CTRL-K CTRL-Y CTRL-Y[凯文·康纳]:

我看到了,这是我自己想知道如何为Emacs做的事情。在ReSharper中,我使用CTRL-D进行此操作。在Emacs中执行此操作的最少命令数是多少?

因为我不知道,我将以慢球开始这轮高尔夫:

ctrl-k,y,y

ctrl-k,ctrl-k,(定位到新位置)ctrl-y


如果不是从行首开始,请添加ctrl-a。第二个ctrl-k是获取换行符。如果您只需要文本,可以将其删除。

将光标放在行上,如果不是在开始时按CTRL-a键,则:

CTRL-K

CTRL-K

CTRL-Y


CTRL-Y[凯文·康纳]:据我所知,非常接近。唯一需要考虑的是打开代码< >杀死整行<代码>,在C-K.

中使用换行符> P>我使用

C-a C-k C-k C-y C-y
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中总是有一个定制<默认情况下,code>C-d绑定到
delete char
,那么
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-d
来做其他事情。

除了前面的答案,您还可以定义自己的函数来复制一行。例如,将以下内容放入.emacs文件将使C-d复制当前行

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

我已将上面的
复制命令
绑定到一个键并使用该键。它提供了XEMAC,但我不知道GNU Emacs

`“从上面复制”命令是一个 交互式编译Lisp函数
--从“/usr/share/xemacs/21.4.15/lisp/misc.elc”加载 (从上面复制命令&可选) ARG)

文档:从中复制字符 上一个非空行,刚开始 以上这一点。复制ARG字符,但是 没有超过那条线的尽头。如果没有 给出一个参数,复制其余的 排队等候。复制的字符是 在点之前插入缓冲区


Nathan添加到.emacs文件是一个不错的选择,但是可以通过替换

  (open-line 1)
  (next-line 1)

屈服

(defun duplicate-line()
  (interactive)
  (move-beginning-of-line 1)
  (kill-line)
  (yank)
  (newline)
  (yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)

您可能希望在.emacs中包含的内容是

(setq kill-whole-line t)
这基本上会在调用kill-line(即通过C-k)时杀死整行加上换行符。然后,不需要额外的代码,您只需执行C-a C-k C-y C-y来复制该行。它分解为

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.

但是如果您经常使用这一功能,那么使用专用密钥绑定可能是一个更好的主意,但是只使用C-a C-k C-y C-y C-y的优点是您可以在其他地方复制该行,而不是在当前行的正下方。

默认设置非常糟糕。但是,您可以将Emacs扩展为像SlickEdit和TextMate一样工作,即在未选择任何文本时复制/剪切当前行:

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

我不太记得在其他任何地方复制行是如何工作的,但作为以前的SciTE用户,我喜欢SciTE方式的一点:它不触摸光标位置! 所以上面所有的回答对我来说都不够好,下面是我的嬉皮版:

(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))))
请注意,在这个过程中并没有任何内容被实际删除,标记和当前选择保持不变


顺便说一句,为什么你们这些家伙这么喜欢在有这么好的“不”干净的整行删除(C-s-backspace)的情况下乱动光标?

我的函数版本可以复制一行,这行可以很好地使用撤销,并且不会弄乱光标的位置。这是一场灾难的结果

然后可以定义CTRL-D来调用此函数:

(global-set-key (kbd "C-d") '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)

使用以下命令代替
C-a
C-k
C-k
C-k
C-y
C-y


C-k
相比,它的优点包括,点在线路的什么位置都不重要(不像
C-k
要求在线路的开头),而且它也会扼杀新线(同样是
C-k
没有做到的事情)。

我喜欢弗拉戈德的版本,除了两件事:(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)))))

使用前缀参数,以及(我希望)直觉行为:

(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))
我发现自己经常使用这两种方法,使用包装器函数切换前缀参数的行为

和一个键绑定:
(全局设置键(kbd“C-S-d”)'重复行)

这里还有另一个用于执行此操作的函数。我的版本没有触及杀人环,光标在新行结束,原来的位置。如果我
(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))
(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)))))
(global-set-key [?\C-c ?d] 'duplicate-line-or-region)
M-w C-a RET C-y
(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))
     (message "Killed line")
     (list (line-beginning-position)
           (line-beginning-position 2)))))
(defun move-line-up ()
  "Move the current line up."
  (interactive)
  (transpose-lines 1)
  (forward-line -2)
  (indent-according-to-mode))

(defun move-line-down ()
  "Move the current line down."
  (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)
  (kill-whole-line)
  (yank)
  (yank))
(global-set-key (kbd "C-x M-d") 'duplicate-line)
(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))
(global-set-key (kbd "M-D") 'wrx/duplicate-line-or-region)
(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 line))
      )))

(global-set-key (kbd "C-S-o") 'duplicate-lines)
fun duplicate-line ()
  (interactive)
  (let ((col (current-column)))
    (move-beginning-of-line 1)
    (kill-line)
    (yank)
    (newline)
    (yank)
    (move-to-column col)))
SPC x l d