How do I duplicate a whole line in Emacs?

前端 未结 30 967
时光取名叫无心
时光取名叫无心 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:23

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

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

    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)

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

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

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

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

    ctrl-k, y, y

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

    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
    
    0 讨论(0)
提交回复
热议问题