是否有Emacs功能可以复制包括换行符在内的整行?

时间:2011-04-20 09:05:33

标签: emacs copy newline

我无法编写emacs功能,有人可以帮助我吗?。

4 个答案:

答案 0 :(得分:4)

是的,有。 This code is borrowed from this blog entry

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

答案 1 :(得分:2)

有几种方法可以复制一行:

  1. 通常的方式: C-a C-SPC C-n M-w

  2. 使用鼠标:三次单击该行,键入 M-w

  3. 将变量kill-whole-line设置(或自定义)为t,然后通过删除和撤消进行复制: Ca Ck C -_

  4. 我没有发现这是一个足够普通的操作,我想给它分配一个组合键,但如果你这样做,那么编写一个函数很容易:

    (defun kill-ring-save-line () 
      "Save the line containing point to the kill ring."
      (interactive)
      (kill-ring-save (line-beginning-position) 
                      (line-beginning-position 2)))
    

答案 2 :(得分:0)

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

C-a C-u 1 C-k C-y

通过发现 C-h k C-k

C-h f kill-TAB

(defun copy-line ()
  (interactive)
  (beginning-of-line)
  (kill-line 1)
  (yank))

答案 3 :(得分:0)

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