複製當前行

複製當前行c++

這是個常常要用到的操做,之前要麼老老實實 Mark 當前行的行首和行尾,而後複製。整個按鍵流程是:ide

  1. Ctrl-a 光標到行首
  2. Ctrl-Shift-Space 設置標記
  3. Ctrl-e 光標到行尾。如此這一行就被選爲激活的區域了
  4. Alt-w 複製當前激活的區域

要麼按我比較習慣的操做先剪切當前行,再撤消上一次的剪切操做spa

  1. Ctrl-a 光標到行首
  2. Ctrl-k 剪切至行屋,該行消失
  3. Ctrl-/ 撤消上一次的操做,該行重現

能夠看到,方案二比方案一省一次按鍵,並且 Ctrl 鍵不用鬆開。不過,如此基本的操做要按三個鍵仍是太麻煩了,並且方案二會讓文件變成被編輯過的狀態。其實,能夠發揮一下「按我說的作」的精神。爲何不把 Alt-w 變的更聰明一些,當沒有激活的區域時就複製當前的一整行呢? 說作就作:
emacs

;; Smart copy, if no region active, it simply copy the current whole line
(defadvice kill-line (before check-position activate)
  (if (member major-mode
              '(emacs-lisp-mode scheme-mode lisp-mode
                                c-mode c++-mode objc-mode js-mode
                                latex-mode plain-tex-mode))
      (if (and (eolp) (not (bolp)))
          (progn (forward-char 1)
                 (just-one-space 0)
                 (backward-char 1)))))
 
(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)))))
 
;; Copy line from point to the end, exclude the line break
(defun qiang-copy-line (arg)
  "Copy lines (as many as prefix argument) in the kill ring"
  (interactive "p")
  (kill-ring-save (point)
                  (line-end-position))
                  ;; (line-beginning-position (+ 1 arg)))
  (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
 
(global-set-key (kbd "M-k") 'qiang-copy-line)

上面還多加了一個配置,就是把 Alt-k 設成複製光標所在處到行尾。與 kill-line 的 Ctrl-k 對應。這樣一來,若是是要拷貝一整行的話,只要將光標移動到該行任意位置,按下 Alt-w 就好了。若是是複製某個位置到行尾的文字的話,就把光標移到起始位置處,按 Alt-k 。比默認的操做簡化了不少。it

相關文章
相關標籤/搜索