How do I duplicate a whole line in Emacs?

前端 未结 30 1077
时光取名叫无心
时光取名叫无心 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条回答
  •  Happy的楠姐
    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)
    

提交回复
热议问题