2016-01-22 62 views
1

Emacs支持M-x find-grep,它搜索字符串並打開兩個緩衝區。一個包含搜索結果的緩衝區和另一個打開包含搜索字符串的文件。定義自定義emacs find-grep快捷方式

當前M-x find-grep擴展爲以下命令Run find (like this): find . -type f -exec grep -nH -e {} +

如何修改查找,grep的(或定義一個新的快捷方式?),它增添了更多選項到grep和find命令

(如忽略日誌文件或僅包括java文件find . -iname '*.java'

回答

0

不要修改find-grep。編寫自己的類似命令,如果你喜歡,可以從代碼的副本開始,而不是調用程序find來實現find . -type f -exec grep -nH -e() +的部分,替換你自己的首選命令行。 ,find . -iname '*.java'

findgrep都有自己的語言(語法) - find,特別是。要使用它們,你需要知道(1)你想要做什麼和(2)如何用他們的語言來做到這一點。

除非您明確指出您正在嘗試執行的操作,否則我們在此提供的唯一幫助是關於從Emacs調用findgrep的一般指導。爲此,find-grep代碼是一個很好的指導 - 參見上文。

+0

感謝您的回覆。 我該如何着手編寫find-grep。說'java-find-grep',它包括一個額外的選項來查找命令('-iname'* .java'') – user462455

+0

正如我所說的,複製'find-grep'的定義,將副本的命令名更改爲'java-find-grep',並替換'find-grep'定義中使用的'find'命令。要查找'find-grep'的定義,請使用'C-h f find-grep RET'。 – Drew

+0

'C-h f find-grep RET'給出了find-grep的描述,但我無法找到該函數的源代碼。 – user462455

0

快速和骯髒的方式是前綴find-grep,即C-u M-x find-grep。它允許您在執行之前編輯命令行。

如果你想永久改變它,你可以定義一個包裝器。這是爲rgrep,但find-grep應該是相似的。

(defvar grep-context-lines 2 
    "Default number of context lines (non-matching lines before and 
    after the matching line) for `rgrep-context'.") 

(defun rgrep-context (arg) 
    "Like `rgrep', but adds a '-C' parameter to get context lines around matches. 

Default number of context lines is `grep-context-lines', and can 
be specified with a numeric prefix." 
    (interactive "p") 
    (setq arg (or arg grep-context-lines)) 
    (let ((grep-find-template 
     (format "find <D> <X> -type f <F> -print0 | xargs -0 -e grep <C> -nH -C %d -e <R>" 
       arg)) 
     grep-host-defaults-alist 
     current-prefix-arg) 
    (call-interactively 'rgrep)))