4

Suppose that I have several files that have a common expression (e.g. software version, authors or function and its manual). I want to change this expression in one file and the other files update automatically.

Is there any way to do that in eclipse or any editor? or is there any software that can do this?

Something like this:

http://wordprocessing.about.com/od/wordquicktips/qt/linktext.htm

In fact, I am looking for something like Code refactoring and I need it only for one user.

I want that the files update instantly.

user1436187
  • 3,252
  • 3
  • 26
  • 59
  • You can use a regular expression to do that. Most editors allow you to replace text based on a regular expression over all files in a path. So in order to show you how it would work, you have to chose an editor and say what is your strings. If it is just for the replace I would recommend Notepad++ – Jorge Campos Oct 27 '13 at 03:58
  • Depending on how far do you want to take it, [inotify](http://en.wikipedia.org/wiki/Inotify) may be the right tool for it. This is a Linux kernel utility function for monitoring the changes made to files. You would then execute your handler in response to the modification. However, your question creates additional problems, which you don't mention, such as: how many people will be able to update files? Is the update centralized or distributed? How fast should the update propagate (realtime, once per compilation, once per day)? –  Oct 30 '13 at 18:53
  • In fact I will be the only programmer. – user1436187 Oct 31 '13 at 03:58
  • Is it important that the change be made to other files before you open them? In other words, is it possible that the editor will update the files the moment they are opened, rather then having to update them instantly, when the change is made to one of the files? Which file gets to decide which is the correct version? –  Oct 31 '13 at 08:57
  • Here's something specific for Emacs: http://www.gnu.org/software/emacs/manual/html_mono/dbus.html#Signals and here's something you may find interesting too: http://schettino72.wordpress.com/2010/03/07/inotify-text-editors-emacs-vim/ –  Oct 31 '13 at 09:08
  • In fact, I look for something like the hard link in linux that exist for the files. But I want to link the text in one or several files. So whenever I change one of them the rest will be updated automatically. – user1436187 Oct 31 '13 at 09:08
  • That's not really a good policy, if I may. If a change to any file should propagate the change to all other files, this means that you need to record what files have to be modified in each file... Centralized management (have master file, which if updated tells other files to update) will require a lot less effort. One other thing: you may want to look into noweb system: http://www.cs.tufts.edu/~nr/noweb/ it is somewhat like a hierarchical include, it would be able to handle updates to multiple files, but you'd need to program accordingly. –  Oct 31 '13 at 18:26

4 Answers4

2

There are many ways to do this using Emacs. One simple one is to use Q in Dired.

See the Emacs manual, node Query Replace (C-h r g Query Replace) for information about the standard query-replace features, including across multiple files.

Here are other Emacs search-and-replace possibilities.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • I like the combination of `rgrep` and [`wgrep`](https://github.com/mhayashi1120/Emacs-wgrep) for this sort of thing, although the question is certainly looking for a more automated approach. – phils Oct 31 '13 at 23:12
1

This is not something you would find in most editor:
The feature you describe in WordProcessing would consist of leaving an "include link" in your text file, which would make your file automatically composed with:

  • the content if a linked file
  • the rest of the content of your file

It is best to leave that kind of automated composition to an external tool which would:

  • parse a template file with the right value
  • parse your file and check that the template file is there (or would replace its content)

That can be automated in the context of a version control tool like git, which has content filter drivers made precisely for that kind of scenario:

smudge

When you checkout a branch, git would automatically call the "smudge" script you have previously declared in a .gitattributes in order to apply it on all the files you want to be updated.
If you modify your template file, and checkout again your branch, all your files will reflect the new content.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. Do you know how can we do that in eclipse with `egit`? – user1436187 Oct 30 '13 at 00:52
  • @user1436187 not sure egit/jgit support content filter driver (http://stackoverflow.com/a/10426607/6309). You would have to do a `git checkout` through command line, and then sync your Eclipse workspace. – VonC Oct 30 '13 at 06:53
0

This kind of thing is easy to do with vim/gvim. This works:

:set nomore
:n `git ls-files`
:argdo sil! %s;<pattern>;<replace>;g | update
cforbish
  • 8,567
  • 3
  • 28
  • 32
0

Not quite automatic but still

  • Ctrl+Shift+H in Visual Studio
  • Ctrl+Shift+F in Notepad++ and Sublime Text

allow replacing in all files in a directory with ability to filter by file type and optionally use regex.

Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90