代码之家  ›  专栏  ›  技术社区  ›  Ray

如何在Emacs中复制整行?

  •  136
  • Ray  · 技术社区  · 16 年前

    我看见了 this same question for VIM 这也是我自己想知道如何为Emacs做的事情。在Resharper中,我使用ctrl-d执行此操作。在Emacs中执行此命令的最少命令数是多少?

    32 回复  |  直到 16 年前
        1
  •  133
  •   Charles Duffy    16 年前

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

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

    C-a C-k C-k C-y C-y
    

    • C-k

    C-d delete-char C-c C-d .emacs

    (global-set-key "\C-c\C-d" "\C-a\C- \C-n\M-w\C-y")
    

        2
  •  91
  •   Nate    16 年前

    (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)
    
        3
  •  58
  •   Hulk1991 moinudin    12 年前

        4
  •  50
  •   some    16 年前

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

    (global-set-key (kbd "C-d") 'duplicate-line)
    
        5
  •  42
  •   Ray    15 年前

    kill-line C-k C-a C-y kill-whole-line

    C-S-Backspace
    C-y
    C-y
    

        6
  •  24
  •   qmega    14 年前

    (defun duplicate-line-or-region (&optional n)
      "Duplicate current line, or region if active.
    With argument N, make N copies.
    With negative N, comment out original line and use the absolute value."
      (interactive "*p")
      (let ((use-region (use-region-p)))
        (save-excursion
          (let ((text (if use-region        ;Get region if active, otherwise line
                          (buffer-substring (region-beginning) (region-end))
                        (prog1 (thing-at-point 'line)
                          (end-of-line)
                          (if (< 0 (forward-line 1)) ;Go to beginning of next line, or make a new one
                              (newline))))))
            (dotimes (i (abs (or n 1)))     ;Insert N times, or once if not specified
              (insert text))))
        (if use-region nil                  ;Only if we're working with a line (not a region)
          (let ((pos (- (point) (line-beginning-position)))) ;Save column
            (if (> 0 n)                             ;Comment out original with negative arg
                (comment-region (line-beginning-position) (line-end-position)))
            (forward-line 1)
            (forward-char pos)))))
    

    C-c d

    (global-set-key [?\C-c ?d] 'duplicate-line-or-region)
    

    C-c

        7
  •  16
  •   pw.    16 年前

      (open-line 1)
      (next-line 1)
    

      (newline)
    

    (defun duplicate-line()
      (interactive)
      (move-beginning-of-line 1)
      (kill-line)
      (yank)
      (newline)
      (yank)
    )
    (global-set-key (kbd "C-d") 'duplicate-line)
    
        8
  •  5
  •   mk-fg    16 年前

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

        9
  •  5
  •   user2626414    10 年前

        10
  •  4
  •   easeout    16 年前

        11
  •  4
  •   viam0Zah    14 年前

    copy-from-above-command


        12
  •  3
  •   jamesh    16 年前
    C-a C-k C-k C-y C-y
    
        13
  •  3
  •   polyglot    16 年前

    (setq kill-whole-line t)
    

    C-a go to beginning of line
    C-k kill-line (i.e. cut the line into clipboard)
    C-y yank (i.e. paste); the first time you get the killed line back; 
        second time gives the duplicated line.
    

        14
  •  3
  •   Marius Andersen Trey Jackson    16 年前

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

    .emacs M-w C-w C-a M-w C-y C-y C-y ...

        15
  •  3
  •   Joyer    16 年前

    duplicate-line

      (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)
    
        16
  •  3
  •   Louis Kottmann    11 年前

    M-w C-a RET C-y
    
        17
  •  3
  •   songyuanyao    9 年前

    (defun duplicate-line ()
      "Duplicate current line"
      (interactive)
      (kill-whole-line)
      (yank)
      (yank))
    (global-set-key (kbd "C-x M-d") 'duplicate-line)
    
        19
  •  2
  •   Allen    16 年前

    kill-whole-line

        20
  •  2
  •   phils    15 年前

    (interactive "*")

    (defun duplicate-line ()
      "Clone line at cursor, leaving the latter intact."
      (interactive "*")
      (save-excursion
        ;; The last line of the buffer cannot be killed
        ;; if it is empty. Instead, simply add a new line.
        (if (and (eobp) (bolp))
            (newline)
          ;; Otherwise kill the whole line, and yank it back.
          (let ((kill-read-only-ok t)
                deactivate-mark)
            (toggle-read-only 1)
            (kill-whole-line)
            (toggle-read-only 0)
            (yank)))))
    
        21
  •  2
  •   prnsml Lol4t0    7 年前

    (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))
         (message "Killed line")
         (list (line-beginning-position)
               (line-beginning-position 2)))))
    

    (defun move-line-up ()
      "Move up the current line."
      (interactive)
      (transpose-lines 1)
      (forward-line -2)
      (indent-according-to-mode))
    
    (defun move-line-down ()
      "Move down the current line."
      (interactive)
      (forward-line 1)
      (transpose-lines 1)
      (forward-line -1)
      (indent-according-to-mode))
    
    (global-set-key [(meta shift up)]  'move-line-up)
    (global-set-key [(meta shift down)]  'move-line-down)
    
        22
  •  1
  •   Hulk1991 moinudin    12 年前

        23
  •  1
  •   kuanyui    10 年前

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

    (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)
    
        24
  •  1
  •   AesopHimself    8 年前

    (defun wrx/duplicate-line-or-region (beg end)
      "Implements functionality of JetBrains' `Command-d' shortcut for `duplicate-line'.
       BEG & END correspond point & mark, smaller first
       `use-region-p' explained: 
       http://emacs.stackexchange.com/questions/12334/elisp-for-applying-command-to-only-the-selected-region#answer-12335"
      (interactive "r")
      (if (use-region-p)
          (wrx/duplicate-region-in-buffer beg end)
        (wrx/duplicate-line-in-buffer)))
    

    (defun wrx/duplicate-region-in-buffer (beg end)
      "copy and duplicate context of current active region
       |------------------------+----------------------------|
       |        before          |           after            |
       |------------------------+----------------------------|
       | first <MARK>line here  | first line here            |
       | second item<POINT> now | second item<MARK>line here |
       |                        | second item<POINT> now     |
       |------------------------+----------------------------|
       TODO: Acts funky when point < mark"
      (set-mark-command nil)
      (insert (buffer-substring beg end))
      (setq deactivate-mark nil))
    

    (defun wrx/duplicate-line-in-buffer ()
      "Duplicate current line, maintaining column position.
       |--------------------------+--------------------------|
       |          before          |          after           |
       |--------------------------+--------------------------|
       | lorem ipsum<POINT> dolor | lorem ipsum dolor        |
       |                          | lorem ipsum<POINT> dolor |
       |--------------------------+--------------------------|
       TODO: Save history for `Cmd-Z'
       Context: 
       http://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs#answer-551053"
      (setq columns-over (current-column))
      (save-excursion
        (kill-whole-line)
        (yank)
        (yank))
      (let (v)
        (dotimes (n columns-over v)
          (right-char)
          (setq v (cons n v))))
      (next-line))
    

    (global-set-key (kbd "M-D") 'wrx/duplicate-line-or-region)
    
        25
  •  0
  •   Karthik    14 年前

    (defun duplicate-line (&optional arg)
      "Duplicate it. With prefix ARG, duplicate ARG times."
      (interactive "p")
      (next-line 
       (save-excursion 
         (let ((beg (line-beginning-position))
               (end (line-end-position)))
           (copy-region-as-kill beg end)
           (dotimes (num arg arg)
             (end-of-line) (newline)
             (yank))))))
    

    (defun duplicate-line (&optional arg)
      "Duplicate it. With prefix ARG, duplicate ARG times."
      (interactive "p")
      (save-excursion 
        (let ((beg (line-beginning-position))
              (end 
               (progn (forward-line (1- arg)) (line-end-position))))
          (copy-region-as-kill beg end)
          (end-of-line) (newline)
          (yank)))
      (next-line arg))
    

    (global-set-key (kbd "C-S-d") 'duplicate-line)

        26
  •  0
  •   WisdomFusion    12 年前
    ;; 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.
    

        27
  •  0
  •   linbianxiaocao    11 年前

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

        28
  •  0
  •   Dodgie    7 年前

    C-3 C-S-o

    (defun duplicate-lines (arg)
      (interactive "P")
      (let* ((arg (if arg arg 1))
             (beg (save-excursion (beginning-of-line) (point)))
             (end (save-excursion (end-of-line) (point)))
             (line (buffer-substring-no-properties beg end)))
        (save-excursion
          (end-of-line)
          (open-line arg)
          (setq num 0)
          (while (< num arg)
            (setq num (1+ num))
            (forward-line 1)
            (insert-string line))
          )))
    
    (global-set-key (kbd "C-S-o") 'duplicate-lines)
    
        29
  •  0
  •   Andy    6 年前

    <C-S-backspace>

    C-/

    <C-S-backspace> C-/

        30
  •  0
  •   Shim Kporku    6 年前

    推荐文章