Change the default find-grep command in emacs

半世苍凉 提交于 2020-01-02 04:08:06

问题


When I execute find-grep command in emacs, I got find . -type f -exec grep -nH -e {} +, since I'm using fish shell as the default shell, to make this command work I have to execute find . -type f -exec grep -nH -e \{\} +.

I tried to modify the emacs source code, below are my changes:

/usr/share/emacs/24.4/lisp/ldefs-boot.el line 12669: If `exec-plus' use `find -exec \{\} +'.

/usr/share/emacs/24.4/lisp/loaddefs.el line 12669: If `exec-plus' use `find -exec \{\} +'.

But it doesn't make any sense, when I execute find-grep still shows find . -type f -exec grep -nH -e {} +. Can anyone tell me where I am doing wrong or how should I figure out this?


回答1:


The text you changed does not look like executable code. Probably you just changed a doc string (actually, a bit of googling reveals that this is in the documentation string for grep-find-use-xargs). But Emacs is eminently customizable; all you have to do is to set the value of grep-find-template to something which is more suitable for you personally, in your own .emacs/init.el or similar.

(setq grep-find-template
      "find <D> <X> -type f <F> -exec grep <C> -nH -e <R> \\{\\} +")

See the manual for further documentation and, of course, the built-in documentation (ctrl-h v grep-find-template RET).

The actual source code is in http://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/progmodes/grep.el#n174 but you really, really, really do not want to edit the source code. The user-level customizability without code changes is one of the foundational designs of Emacs. Learn to use this facility.




回答2:


You need to use the function grep-apply-setting to set the variable grep-find-command, and double up on the backslashes before the curly braces:

(grep-apply-setting 'grep-find-command "find . -type f -exec grep -nH -e  \\{\\} +")



回答3:


(grep-apply-setting 'grep-find-command '("find . -type f -exec grep -nH -e  \\{\\} +" . 34))

Will place the cursor on just slightly after the -e




回答4:


If you want to preserve the original value you can do something like this. It will find files no further down than two directory levels, that are not emacs backup files, and have been modified in the past week.

(defun grep-find-newer-shallow ()
  (interactive)
  (let ((gfc grep-find-command))
    (grep-apply-setting 'grep-find-command '("find . -maxdepth 2 -type f ! -name '*#' -mtime -7 -exec grep -nH -e  \\{\\} +" . 69))
    (call-interactively 'grep-find)
    (grep-apply-setting 'grep-find-command gfc)))


来源:https://stackoverflow.com/questions/28915372/change-the-default-find-grep-command-in-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!