如何在 Emacs 中复制整行?

发布于 2024-07-05 16:29:30 字数 192 浏览 9 评论 0原文

我看到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

南冥有猫 2024-07-12 16:29:31

相对于克里斯·康威选择的答案,这感觉更自然。

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

这样,您只需重复 Ctrl-c Ctrl-d 按键即可多次复制一行。

This feels more natural, with respect to the selected answer by Chris Conway.

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

This allows you to duplicate a line multiple times by simply repeating the Ctrl-c Ctrl-d key strokes.

沦落红尘 2024-07-12 16:29:31

我根据自己的喜好写一篇。

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

I write one for my preference.

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

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:

(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)
提赋 2024-07-12 16:29:31

这是复制当前行的函数。 使用前缀参数,它将多次重复该行。 例如,C-3 CSo 会将当前行复制三遍。 不改变杀环。

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

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.

(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)
玩世 2024-07-12 16:29:31

如果您使用的是 Spacemacs,则只需使用 duplicate-line-or-region 即可,绑定到:

SPC x l d 

If you're using Spacemacs, you can simply use duplicate-line-or-region, bound to:

SPC x l d 
向日葵 2024-07-12 16:29:31

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.

风追烟花雨 2024-07-12 16:29:31

ctrl-kctrl-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.

西瑶 2024-07-12 16:29:31

正如其他答案中提到的,将击键绑定到 lisp 代码比将它们绑定到另一个击键更好。 根据 @mw 的回答,代码复制该行并将标记移动到新行的末尾。 此修改将标记位置保持在新行的同一列:

fun duplicate-line ()
  (interactive)
  (let ((col (current-column)))
    (move-beginning-of-line 1)
    (kill-line)
    (yank)
    (newline)
    (yank)
    (move-to-column col)))

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:

fun duplicate-line ()
  (interactive)
  (let ((col (current-column)))
    (move-beginning-of-line 1)
    (kill-line)
    (yank)
    (newline)
    (yank)
    (move-to-column col)))
忆伤 2024-07-12 16:29:30

我无法相信所有这些复杂的解决方案。 这是两次击键:

运行命令kill-whole-line

C-/ 运行命令undo

因此; C-/ “复制”整行(终止和撤消)。

当然,您可以将其与数字和负参数结合起来,以向前或向后杀死多行。

I cannot believe all these complicated solutions. This is two keystrokes:

<C-S-backspace> runs the command kill-whole-line

C-/ runs the command undo

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

空气里的味道 2024-07-12 16:29:30

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

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:

(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)))))
红ご颜醉 2024-07-12 16:29:30

使用最近的 emacs,您可以在行中的任何位置使用 Mw 来复制它。 所以就变成了:

M-w C-a RET C-y

With recent emacs, you can use M-w anywhere in the line to copy it. So it becomes:

M-w C-a RET C-y
萌吟 2024-07-12 16:29:30

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

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

I saw very complex solutions, anyway...

(defun duplicate-line ()
  "Duplicate current line"
  (interactive)
  (kill-whole-line)
  (yank)
  (yank))
(global-set-key (kbd "C-x M-d") 'duplicate-line)
止于盛夏 2024-07-12 16:29:30

当没有活动区域交互调用时,改为 COPY (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)))))

当没有活动区域交互调用时,改为 KILL (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 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)

When called interactively with no active region, COPY (M-w) a single line instead :

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

When called interactively with no active region, KILL (C-w) a single line instead.

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

Also, on a related note:

(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)
说谎友 2024-07-12 16:29:30

@[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.

通知家属抬走 2024-07-12 16:29:30

我不太记得行复制在其他地方是如何工作的,但作为一名前 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-退格键)时晃动光标?

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:

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

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

软的没边 2024-07-12 16:29:30

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

梅窗月明清似水 2024-07-12 16:29:30

而不是 Ca 中的 kill-line (Ck) Ck Ck Cy Cy 使用 kill-whole-line 命令:

C-S-Backspace
C-y
C-y

相对于 Ck 的优点包括,点在线上的位置并不重要(与 Ck 不同) >Ck 需要位于行的开头)并且它还会杀死换行符(同样是 Ck 不做的事情)。

Instead of kill-line (C-k) as in C-a C-k C-k C-y C-y use the kill-whole-line command:

C-S-Backspace
C-y
C-y

The advantages over C-k include that it does not matter where point is on the line (unlike C-k which requires being at start of the line) and it also kills the newline (again something C-k does not do).

夏雨凉 2024-07-12 16:29:30

我使用

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

它分解为

  • Ca:将光标移动到行首
  • C-SPACE:开始选择(“设置标记”)
  • Cn:将光标移至下一行
  • Mw:复制区域
  • Cy:粘贴(“yank”)

上述

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

内容相同 (TMTOWTDI)

  • Ca:将光标移动到行首
  • Ck:剪切(“kill”)该行
  • Ck:剪切换行
  • Cy:粘贴(“yank”) (我们回到了第一个位置)
  • Cy:再次粘贴(现在我们有该行的两个副本)

与编辑器中的 Cd 相比,这些都冗长得令人尴尬,但在 Emacs 中总是有自定义。 Cd 默认绑定到 delete-char,那么 Cc Cd 又如何呢? 只需将以下内容添加到您的 .emacs 中即可:

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

(@Nathan 的 elisp 版本可能更可取,因为如果更改任何键绑定,它也不会中断。)

请注意:某些 Emacs 模式可能会回收 抄送 Cd 去做其他事情。

I use

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

which breaks down to

  • C-a: move cursor to start of line
  • C-SPACE: begin a selection ("set mark")
  • C-n: move cursor to next line
  • M-w: copy region
  • C-y: paste ("yank")

The aforementioned

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

amounts to the same thing (TMTOWTDI)

  • C-a: move cursor to start of line
  • C-k: cut ("kill") the line
  • C-k: cut the newline
  • C-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 to delete-char by default, so how about C-c C-d? Just add the following to your .emacs:

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

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

孤单情人 2024-07-12 16:29:30

除了前面的答案之外,您还可以定义自己的函数来复制行。 例如,将以下内容放入 .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)

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.

(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)
你如我软肋 2024-07-12 16:29:30

将光标放在行上,如果不在开头执行 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

只为守护你 2024-07-12 16:29:30

我的复制行函数版本可以很好地进行撤消操作,并且不会扰乱光标位置。 这是 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)

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.

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

Then you can define CTRL-D to call this function:

(global-set-key (kbd "C-d") 'duplicate-line)
爱你是孤单的心事 2024-07-12 16:29:30

这是执行此操作的另一个函数。 我的版本没有触及终止环,并且光标最终位于原始版本上的新行上。 如果它处于活动状态(瞬态标记模式),它将复制该区域,否则默认复制该行。 如果给定前缀 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)))))

我将其绑定到Cc d

(global-set-key [?\C-c ?d] 'duplicate-line-or-region)

这永远不应该由模式或任何东西重新分配,因为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).

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

I have it bound to C-c d:

(global-set-key [?\C-c ?d] 'duplicate-line-or-region)

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.

夜深人未静 2024-07-12 16:29:30

Nathan 添加到 .emacs 文件中是可行的方法,但可以通过替换

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

  (newline)

Yieling来稍微简化

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

Nathan's addition to your .emacs file is the way to go but it could be simplified slightly by replacing

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

with

  (newline)

yielding

(defun duplicate-line()
  (interactive)
  (move-beginning-of-line 1)
  (kill-line)
  (yank)
  (newline)
  (yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)
夜光 2024-07-12 16:29:30

默认值对于这个来说是可怕的。 但是,您可以扩展 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 中。 然后,要复制一行,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:

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

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

断舍离 2024-07-12 16:29:30

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

ctrl-k,y,y

because i don't know, i'll start this round of golf with a slowball:

ctrl-k, y, y

寄人书 2024-07-12 16:29:30

有一个名为 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:

enter image description here

醉南桥 2024-07-12 16:29:30

此功能应与 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)

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:

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

Which calls this,

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

Or this

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

And then I have this bound to meta+shift+d

(global-set-key (kbd "M-D") 'wrx/duplicate-line-or-region)
忘你却要生生世世 2024-07-12 16:29:30
C-a C-k C-k C-y C-y
C-a C-k C-k C-y C-y
笑忘罢 2024-07-12 16:29:30

您可能希望在 .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 的优点是您可以在其他地方复制该行,而不是在当前行下方。

something you might want to have in your .emacs is

(setq kill-whole-line t)

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

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.

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.

旧城空念 2024-07-12 16:29:30

我将 copy-from-above-command 绑定到一个键并使用它。 XEmacs 提供了它,但我不知道 GNU Emacs。

`copy-from-above-command' 是一个
交互式编译的 Lisp 函数
-- 从“/usr/share/xemacs/21.4.15/lisp/misc.elc”加载
(从上述命令复制和可选
ARG)

文档:复制字符
上一个非空行
,刚刚开始
以上观点。 复制 ARG 字符,但是
没有超过该行的末尾。 如果不
给出参数,复制整个其余部分
的线。 复制的字符是
在点之前插入缓冲区。

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.

`copy-from-above-command' is an
interactive compiled Lisp function
-- loaded from "/usr/share/xemacs/21.4.15/lisp/misc.elc"
(copy-from-above-command &optional
ARG)

Documentation: Copy characters from
previous nonblank line
, starting just
above point. Copy ARG characters, but
not past the end of that line. If no
argument given, copy the entire rest
of the line. The characters copied are
inserted in the buffer before point.

半窗疏影 2024-07-12 16:29:30

'我写了我自己的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)

' I wrote my own version of duplicate-line, cause I don't want to screw up the killing ring.

  (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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文