2016-01-22 64 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)))