occur mode
若只是需要在Emacs所打开的buffer中查询某字段,则occur命令一个对GNU grep的很好的替代.
occur命令
M-x occur会让你输入一个正则表达式,然后在 当前buffer 中进行内容匹配,在Occur Output buffer中显示匹配正则表达式的行,并对匹配部分进行加亮处理.
M-x multi-occur会让你选择要在哪些buffer中进行内容匹配
M-x multi-occur-in-matching-buffers会在buffer name符合某正则的buffer中进行内容匹配
M-x occur-rename-buffer 对默认的*Occur* outputbuffer进行重命名,命名规则为*Occur:匹配的文件名列表,用/分隔*
occur-mode的子命令
r命令重命名*occur output* buffer
g命令重现做一次正则匹配查询
M-g M-n / M-g M-p 遍历*occur output* buffer中匹配行的原所在文件位置
C-c C-f 开启Next-Error-Follow minor mode
在开启Next-Error-Follow minor mode后,可用使用M-n和M-p来代替M-g M-n / M-g M-p
q命令退出*occur output* buffer
让occur命令作用于某个major mode的所有buffer中
(eval-when-compile (require 'cl)) (defun get-buffers-matching-mode (mode) "Returns a list of buffers where their major-mode is equal to MODE" (let ((buffer-mode-matches '())) (dolist (buf (buffer-list)) (with-current-buffer buf (if (eq mode major-mode) (add-to-list 'buffer-mode-matches buf)))) buffer-mode-matches)) (defun multi-occur-in-this-mode () "Show all lines matching REGEXP in buffers with this major mode." (interactive) (multi-occur (get-buffers-matching-mode major-mode) (car (occur-read-primary-args)))) ;; global key for `multi-occur-in-this-mode' - you should change this. (global-set-key (kbd "C-<f2>") 'multi-occur-in-this-mode)
转载于:https://blog.51cto.com/darksun/1416934