复制当前行c++
这是个常常要用到的操做,之前要么老老实实 Mark 当前行的行首和行尾,而后复制。整个按键流程是:ide
要么按我比较习惯的操做先剪切当前行,再撤消上一次的剪切操做spa
能够看到,方案二比方案一省一次按键,并且 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