8

I am about to adopt Emacs to be the only editor I use in all what I am writing including LaTeX documents. I came from WYSIWYG editor which served that purpose fairly nice, namely TexStudio or TXS for short. For the transition from TXS to Emacs I am still missing the way TXS deals with executing massive regexp query search & replace with highlighting the matches and restriction of the search to only those project-related .tex files. I deeply miss that in Emacs now. See a screenshot of TXS below:

TeXStudio Query in a Multifile LaTeX Project enter image description here

I would like to emphasize the points in the workflow to be achieved in Emacs:

  1. RegExp Query Search and Replacement only in the project-related .tex files.
  2. Highlighting the matches
  3. An overview of the highlighted results all together in a manageable layout (similar to the log view of Magit in Emacs when you press l followed by another l or simply like C-x d in Dired mode
  4. How to know how much replacement or inspection matches done with or remaining and a way to resume query in case it is broken by a clumsy keystroke that stops the query
  5. A clean way out of this messy query by closing and saving all related files and back to the state before invoking this query

I read this post but my intention is to be more like CW that anyone can share his best workflow to get this common job done. My current approach(es) as follows:

First Workflow

  1. Dired mode % g will mark all files containing that regexp. Cons: this will include even file unrelated to the project including non .tex files.
  2. C-x 3 opens a new buffer while keeping an eye on the first buffer as my orientation overview of what marked files I have to query in the new buffer individually
  3. Running the query by C-c q. It has very nice switches like ! replace all without asking plus highlighting matches within the opened file. But this means I have to run the same query each time and for every marked file.
  4. After doing all marked files I hit C-x s ! to save all in one keystroke. Phew!

enter image description here

Second Workflow -- preferred

  • Marking all .tex files using % m within Dired-mode by regexp in order to mark only .tex files so basically running \.tex will get me there.
  • C-x 3 same as above to keep one buffer for overview and the other for query
  • Executing Q within Dired which is query replace regexp in marked files only. Alternatively A is only regexp search query. A full-fledged query will be executed and a help menu by ? is generously provided
  • C-x s ! save and close all the mess. Otherwise I had to close the opened files one by one which is tedious. I hope there is a way to close only those we bothered.

enter image description here

How do I approximate TXS's regexp search/replace query in multifiles?

Note

  • Emacs 24.4 on Windows 7
3
  • This question currently has a "close" vote for being too broad, which I'm inclined to agree with. Could you please narrow the question down to a more discrete version (perhaps along the lines of "how do I approximate TeXStudio's regexp query search in multiple files")? "What's your best workflow?" is very broad and is likely to yield lengthy and opinion-based answers.
    – Dan
    Jan 3, 2015 at 19:38
  • I can't write a full answer right now, but you should look into wgrep.
    – Malabarba
    Jan 3, 2015 at 21:38
  • @Malabarba, please consider upgrading your comment to an answer if you think this will satisfy the criteria mentioned in the post.
    – doctorate
    Jan 5, 2015 at 11:16

3 Answers 3

3

If I understood correctly, you want to search for text patterns in your project, then you may want to use Projectile along with Helm.

  • M-x helm-projectile-grep
  • Type text pattern or regex pattern.
  • See updated results for every character entered.

Here is a demo of helm-projectile-grep:

helm-projectile-grep

You can save into a separate buffer to refer later using Save results in grep buffer action` (you switch to action buffer using TAB, by default).

If you haven't heard of Helm, you can read my Helm tutorial. Visit my Helm Projectile tutorial for more demos and guidance on Helm Projectile.

Alternative, you can also use rgrep, which is built-in:

  • M-x rgrep
  • Enter text/regex pattern into Search for: prompt, i.e. "test"
  • Enter file type to search for into Search for "test" in file:, i.e. *.tex.
  • Enter directory to recursively search (hence the name rgrep).
  • Press RET and see results.

A rgrep demo:

enter image description here

In regard to your criteria:

  1. RegExp Query Search and Replacement only in the project-related .tex files.

Both Helm and rgrep support this feature.

  1. Highlighting the matches

Same as above.

  1. An overview of the highlighted results all together in a manageable layout

Same as above.

  1. How to know how much replacement or inspection matches done with or remaining and a way to resume query in case it is broken by a clumsy keystroke that stops the query

If you want to replace matches, you can use wgrep to perform replacement to a *Grep* buffer. Once you finish and sync back the *Grep* buffer, everything will be replaced according to the text you replaced.

You "resume" by running the query again if you accidentally stop it. But using Helm, you have a helm-resume command that resume you back to previous on going operation if you accidentally cancel it.

  1. A clean way out of this messy query by closing and saving all related files and back to the state before invoking this query

Actually, you don't have to kill any buffer if you use tools like helm-mini. Managing thousands of open buffers is a breeze: you can narrow to your desire buffers with a short text pattern. You can kill all at once if you want.

Here is a demo of helm-mini:

helm-mini

See the detail explanation in Helm Github page, in "Advanced Usage" section.

6
  • I think I have to try and study projectile with helm. Thanks for your screenshots-rich tutorials!
    – doctorate
    Jan 3, 2015 at 18:51
  • what packages I need to install to get these decent functions: projectile, helm and helm-projectile is obvious, what else? Plus there are two MELPA pkgs helm-pojectile and helm projectile-all any recommendations here?
    – doctorate
    Jan 3, 2015 at 19:59
  • That's enough already. Then, follow the configuration in my guides. You should check the Installation section in my Helm and Helm Projectile guides.
    – Tu Do
    Jan 3, 2015 at 20:05
  • what keystrokes do you use in rgrep to visit the match occurrences (forward or backward) in the upper and lower buffer?
    – doctorate
    Jan 3, 2015 at 20:06
  • 1
    it was an Emacs-changing experience to meet helm and helm-projectile, Thanks! But, grep search not working. Can you pls care to look at this post: emacs.stackexchange.com/q/7178/2443
    – doctorate
    Jan 8, 2015 at 17:27
5

If I understood what you want, RefTeX' reftex-isearch-minor-mode should be what you are after. From its description in the manual:

Toggle a minor mode which enables incremental search to work globally on the entire multifile document. Files will be searched in the sequence they appear in the document.

As reported by T. Verron, this works with regexps as well.

To enable this minor mode in all *TeX buffer, add one of the following lines to your init file

(add-hook 'TeX-mode-hook (lambda () (reftex-isearch-minor-mode))) ; for AUCTeX
(add-hook 'tex-mode-hook (lambda () (reftex-isearch-minor-mode))) ; for the Emacs' bultin TeX mode.
7
  • Good to know as I use reftex. But since no regexp is supported I doubt this approach would be any better!
    – doctorate
    Jan 3, 2015 at 18:21
  • 2
    Apparently, pressing M-r in the search buffer switches to regexp search, just like it does for regular isearch.
    – T. Verron
    Jan 3, 2015 at 18:51
  • 1
    And the usual shortcut for regexp isearch (C-M-s) does work too.
    – T. Verron
    Jan 3, 2015 at 18:57
  • @T.Verron thank you! Actually I haven't used this feature extensively, the last time I tried it regexp search didn't work, who knows what I tried then ;-)
    – giordano
    Jan 3, 2015 at 19:10
  • @T.Verron from the menu bar->ref->global actions->search version and replace version for whole document provides better regexp as you can edit your expression while in minibuffer but I don't know what function is this and what is the keybinding.
    – doctorate
    Jan 3, 2015 at 19:39
3

In case you are not aware, you can open Dired on only a particular set of files (located in any directories, anywhere). IOW, you can use Dired as your entry point for all of the files of a project. The usual Dired features work in this context: operating on marked files, etc.

If you use library Bookmark+ then you can have Dired bookmarks that you use to give you different "views" of differents sets of files, their markings, etc.

(Emacs Wiki is down for maintenance currently, and for a few more days, so that link is temporarily inaccessible.)

2
  • Is it like Emacs narrowing C-x n n? can you please enrich your answer with more details. Thanks.
    – doctorate
    Jan 3, 2015 at 19:00
  • No; nothing to do with C-x n n - just a little known Dired feature. C-h f dired tells you that you can pass as argument a list of (e.g. absolute) file (and directory) names instead of a directory name. That's the key. Library Dired+ makes it easier to use this interactively. (But again, the wiki is down temporarily, so that link too is not available at the moment.)
    – Drew
    Jan 4, 2015 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.