2011년 11월 11일 금요일

[Emacs] 현재 라인 잘라내기/복사하기

Emacs에서 현재 커서가 위치하고 있는 라인을 복사하려면 일단 커서를 제일 앞으로 이동(C-a)한 후 영역선택을 켜고(C-SPC) 라인 끝으로 이동(C-e)해서 복사(M-w)하는 복잡한(?) 과정을 거쳐야 한다.

Emacs는 무한확장이 가능하다! 즉, 좀 더 간편히 하기 위해 Emacs의 기능을 수정할 수 있다. 아래 스크립트를 실행시키고 난 뒤에는 영역 선택 없이 M-w 키를 누르면 바로 현재 라인이 복사가 되어 킬링(kill-ring)에 들어간다.

(defadvice kill-ring-save (before slick-copy activate compile)
  "When called interactively with no active region, copy the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (message "Current line is copied.")
       (list (line-beginning-position) (line-beginning-position 2)) ) ) ))

(defadvice kill-region (before slick-copy activate compile)
  "When called interactively with no active region, cut the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (list (line-beginning-position) (line-beginning-position 2)) ) ) ))

Vim에서 yy를 이용한 현재 라인 복사하기 기능이 부러웠다면 이 코드를 Emacs 초기화 스크립트(.emacs 등)에 넣어보자.

참고: Emacs 24.2 에서는 kill-region 대신 completion-kill-region이 쓰인다. 따라서 위의 코드에서 kill-region 심볼을 completion-kill-region 으로 바꿔줄 필요가 있다.

원문) http://xahlee.org/emacs/emacs_copy_cut_current_line.html

댓글 없음 :