How do I duplicate a whole line in Emacs?

前端 未结 30 968
时光取名叫无心
时光取名叫无心 2020-12-04 05:20

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 num

相关标签:
30条回答
  • 2020-12-04 05:43
    C-a C-k C-k C-y C-y
    
    0 讨论(0)
  • 2020-12-04 05:43

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

    0 讨论(0)
  • 2020-12-04 05:44

    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)
    
    0 讨论(0)
  • 2020-12-04 05:45

    ' 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)
    
    0 讨论(0)
  • 2020-12-04 05:45

    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)
    
    0 讨论(0)
  • 2020-12-04 05:47

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

    0 讨论(0)
提交回复
热议问题