我看到了 这个同样的问题VIM 它已经被一些东西我自己想知道如何做Emacs。在ReSharper我用CTRL-D这一行动。什么是少数的命令来执行这Emacs?

有帮助吗?

解决方案

我用

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-ddelete-char 通过默认,因此如何 C-c C-d?只是添加以下你 .emacs:

(global-set-key "\C-c\C-d" "\C-a\C- \C-n\M-w\C-y")

(@内森的工作基本是在封闭的情况下进行的版本可能是可取的,因为它会不会打破,如果任何的关键绑定改变。)

要注意:一些Emacs的模式可以收回 C-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)

地方上的光标线,如果没有在开始时做一个 CTRL-一个, ,则:

CTRL-K

CTRL-K

CTRL-Y

CTRL-Y

我的版本的功能重复一线工作的好的撤销和不惹标的位置。这是结果 讨论gnu。emacs。源自1997年.

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

而不是的 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 这需要正在开始的线)和它也杀死newline(再次东西 C-k 不做)。

这是又一个功能这样做。我的版本不会触及的环杀,并将光标出现在新的行它在原来的。它将重复该区域的如果它的活动(瞬态标记方式),或默认要复制线。它还将使多份拷贝如果给出一个前缀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 随后通过一个单(未经修改)的字母是保留给用户绑定。

内森的除了你的.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)

我不太记得如何行重复工作的其他任何地方,但作为一个前赛特户我喜欢一件事关于赛特-方式:它不会触及标的位置!因此,所有的食谱上没有足够好对我来说,这是我的嬉皮的版本:

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

注意,没有被实际杀害过程中,留下痕迹,以及目前的选择完整。

顺便说一句,为什么你们这么喜欢催人泪下的光标的时候有个好'n''clean杀整个行啄(C S-backspace)?

安装重复的事情从melpa:

M-x包装RET重复的事情

并添加到快捷键 init文件 :

(全球机的关键(大骨节病"M-c")复制件事)

因为我不知道,我将开始的这一轮高尔夫球带slowball:

ctrl-k,y,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)

copy-from-above-command 开的一个关键和使用。它提供与XEmacs,但我不知道关于GNU Emacs。

复制-从上面的命令是 交互式编口齿不清的功能
--从"/usr/share/xemacs/21.4.15/list/misc.易乐看" (复制从上述命令和任择 阿根廷)

文件: 复制人物从 前一个非空线, 开始只是 上述一点。复制ARG字,但是 不过这条线。如果没有 参定,复制整个休息 线。人物是复制的 插入缓冲器之前的一点。

C-a C-k C-k C-y C-y

有些东西你可能会想在你的.emacs是

(setq kill-whole-line t)

这基本上可以杀死整个线加newline只要你调用杀线(即通过C-k)。然后无需额外的代码,你可以做到的C-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-C-k C-y C-y是可以复制线的其他地方,而不只是下面的前线。

默认值是可怕的。然而,可以延长Emacs的工作像SlickEdit和突,这是复制/切割的前线时,没有文本选择:

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

我喜欢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)))))

最近软件可以使用M-w任何地方是在线进行复制。所以它就变成:

M-w C-a RET C-y

我看到了非常复杂的解决方案,无论如何...

(defun duplicate-line ()
  "Duplicate current line"
  (interactive)
  (kill-whole-line)
  (yank)
  (yank))
(global-set-key (kbd "C-x M-d") 'duplicate-line)

有包装叫 Avy 它已经命令avy-复制线。当你用那个指令,每个线在你的窗口获得信的组合。然后你只需要类型的组合和你获得该线。这也适用于区域。然后你只需要两个类型的组合。

在这里你可以看到的界面:

http://i68.tinypic.com/24fk5eu.png

@[凯文*康纳]:漂亮的接近,到目前为止,我知道。只有其他的事情要考虑的是打开 kill-whole-line 包括newline在C-k。

当所谓的交互式没有活动地区,复制(M-w)一个单一的线来代替:

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

当所谓的交互式没有活动地区,杀(C-w)一个单一的线来代替。

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

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

增加一个 ctrl-一个 如果你不开始在开始的线路。和第2 ctrl-k 被抓住的newline character.它可以删除,如果你只想要的文字。

我写一个为我的优先选择。

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

但是我找到了这将有一些问题当前线包含多字(例如中日韩字符)。如果你遇到这个问题的,试试这个代替:

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

这个功能应符合本文执行情况方面的重复这两线或地区,然后离开点和/或活动的地区的预期:

只是一个包装周围的互动形式:

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

然后我有这个必要元+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))

我发现自己使用这两个经常,使用的包装能够开关的行为前缀的论点。

和一个快捷键:(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.

增加的工作基本是在封闭的情况下进行上述要你init。el,你得到切割/复制全线的功能,现在,然后你可以F3F4来重复的一条线。

最简单的方法是克里斯*康威的方法。

C-a C-SPACE C-n M-w C-y

这是默认的方式授权的EMACS。在我看来,最好使用的标准。我总是小心对定制自己的关键结合在EMACS。EMACS已经足够强大,我认为我们应该尽我们最好适应它自己的钥匙-绑定。

虽然这是一个有点长,但是当你们用于它,你可以做到快速和会发现这很有趣!

这里有一个功能为重复的前线。与前缀参数,它将重复该行多次。E.g., 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)

我不能相信所有这些复杂的解决方案。这是两个按键:

<C-S-backspace> 运行命令杀整线

C-/ 运行命令撤消

所以 <C-S-backspace> C-/ "复制"一体行(杀和撤消).

你当然可以,结合这种数字和负args杀了多条线路向前或向后。

这种感觉更自然的,相对于所选择的答案由克里斯*康威.

(全球机的关键"\C-c\C-d""\C-a\C-\C-n M-w\C-y\C-p\C-e")

这可以让你重复一线多次通过简单地重复\C-c\C-d按键。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top