GuidedKamikaze · 2022-03-20 17:38
I'm new to emacs so this may be a simple thing that I've just overlooked, but I was wondering if there is an easy way to live link the contents of a file?
Say you have the same 10 lines of text in a dozen different files. Is there a way to update all instances of that text at once? Can you link to a file but have the description just be the contents of the file that's linked?
Scr0f3 · 2022-03-20 17:43
I use [https://github.com/nobiot/org-transclusion](https://github.com/nobiot/org-transclusion) for this
pbgc · 2022-03-21 00:05
I use wgrep: https://github.com/mhayashi1120/Emacs-wgrep
arthurno1 · 2022-03-21 07:37
> Say you have the same 10 lines of text in a dozen different files. Is there a way to update all instances of that text at once?
It depends. If you would like to do it interactively in real time as you type, then the answer is no. But if you are ok with some sort of qurey-replace, you can use [Dired](https://www.gnu.org/software/emacs/manual/html_node/efaq/Replacing-text-across-multiple-files.html). More in [this SX answer](https://emacs.stackexchange.com/questions/9495/how-do-i-search-and-replace-over-a-set-of-files). or use Emacs as a front-ed to some shell command like sed, wgrep, etc, or you could write your own lisp function, it is not very hard.
GuidedKamikaze · 2022-03-21 11:34
Thanks! This looks like it may be what I'm looking for.
ieure · 2022-03-21 15:57
I'm sure there are packages and whatnot, but this is another case where there's a great built-in solution. It feels like a lot of folks think Install A Package is the solution to every problem. Emacs is vast and ancient and contains untold wonders; even after 20+ years of using it every day, I'm still finding new stuff I never knew about. It's foolish to go searching for another package (or writing one!) before exploring what's already in front of you.
The built-in solution here is a combination of `find-grep-dired` and `dired-do-find-regexp-and-replace`.
`find-grep-dired` smushes `find-dired` and `grep` together; it uses `find(1)` to locate files; greps those files; and gives you a dired buffer of the files that matched both. You can use this to very precisely identify a set of files -- like every file matching `*_test.clj` that imports `java.net.URL`, or files modified since yesterday owned by me which *don't* contain my email address -- or whatever combination you can come up with.
Once you have the dired buffer up, `Q` runs `dired-do-find-regexp-and-replace`, which lets you do a query-replace across every marked file (or every file, if none are marked) in the current Dired buffer. It works in any dired buffer, not just `find-grep-dired` ones. The bindings are slightly different than `query-replace`, but it works in a similar way; enter the regexp and your replacement, then hit `Y` to apply to every occurrence in every file being operated on.
Then `C-x s !` to save all the modified buffers.
This setup is one of my favorite things in Emacs, because it shows how powerful it can be to have a system built of simple components which interact with each other. For users who already know how to use things like dired and query-replace, those skills transfer directly into a significantly more advanced set of operations. It's good.
Rotatop · 2022-03-21 17:10
Just for a working example :
I use 'doom emacs'
I find my text (I don't remember the name of rhe func, it use 'vertico') with SPC s p ('search in project I think)
Then when all the results are in the minibuffer, I press 'Ctrl-c CTRL-e'
Then I can do all the edit I want on lines (i can also delete them)
Then 'Z Z' to save and quit. And all the lines are updated.
Having the ability to edit multiple file at once is a requirement for me.