How do I duplicate a whole line in Emacs?

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

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

    add the elisp above to you init.el, and you get cut/copy whole line function now, then you can F3 F4 to duplicate a line.

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

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

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

    Place cursor on line, if not at beginning do a CTRL-A, then:

    CTRL-K

    CTRL-K

    CTRL-Y

    CTRL-Y

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

    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.

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

    The simplest way is Chris Conway's method.

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

    That's the default way mandated by EMACS. In my opinion, it's better to use the standard. I am always careful towards customization one's own key-binding in EMACS. EMACS is already powerful enough, I think we should try our best to adapt to its own key-bindings.

    Though it's a bit lengthy, but when you are used to it, you can do fast and will find this is fun!

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