lgrep grep rgrep

1 minute read

M-x lgrep

local grep. 디렉터리에서 찾을 때.

M-x rgrep

recursive grep. 디렉터리 트리에서 찾을 때. 서브 디렉터리 포함이다.

lgrep, rgrep 둘 다 친절하다. 찾으려는 문자열, 파일 이름 패턴, 시작 디렉터리를 차례로 묻는다.

M-x grep

친절은 괜찮소. 명령어를 바로 치고 싶을 때, 사용.

(setq grep-command "grep -nH -i -r ")

기본 명령 인자를 정의할 수 있다.

grep-highlight-matches

(setq grep-highlight-matches 'always)

일치하는 문자열을 강조해서 보여준다.

grep: warning: GREP_OPTIONS is deprecated; please use an alias or script

하지만 더는 사용하지 않는(deprecated) GREP_OPTIONS 환경 변수를 사용해서 warning 메시지 작렬.

(setq grep-template "grep <X> --color=always <C> -nH <R> <F>")
(setq grep-find-template
      "find . <X> -type f <F> -exec grep --color=always <C> -nH <R> {} \\;")

그래서 --color 옵션을 직접 넣어준다. grep-template 변수는 lgrep 함수가 사용하고 grep-find-template 변수는 rgrep 함수가 사용한다.

compilation-mode

(get 'grep-mode 'derived-mode-parent)
;;=> compilation-mode

compilation mode를 상속 받았다.

| C-x ` | next-error |

포커스가 *grep* 버퍼에 없더라도 다음 라인을 찾아갈 수 있다. 함수 이름이 next-error인 건 상속 받은 compilation mode 때문.

init.el

;;; grep
(setq grep-command "grep -nH -i -r ")
;; grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
;; grep-highlight-matches 변수를 세팅하니 경고 메시지 작렬
;; 그래서 고쳐지기 전까지는 --color 옵션을 직접 세팅해준다.
(setq grep-template "grep <X> --color=always <C> -nH <R> <F>")
(setq grep-find-template
      "find . <X> -type f <F> -exec grep --color=always <C> -nH <R> {} \\;")

이렇게 사용 중.

참고

C-x C-s C-x C-c