How do I duplicate a whole line in Emacs?

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

提交回复
热议问题