views:

86

answers:

4

For reasons undisclosed, suppose I have many identical README.txt files in a few dozen sub-directories. In my fantasy world, I could run this:

vim --magic-mindreading-flag */README.txt

and rather than editing the files for me in sequence, vim will somehow recognize that they're identical and save my changes to all files simultaneously and magically know what I want.

Please spare me the following:

  1. How to do this with shell commands.
  2. Lecture on referencing a single authoritative copy (such as with symlinks)
  3. A recommendation to use an editor that you know of which supports it.

This is about vim, and how awesome.

Bonus: get vim will to warn if the files aren't identical.

A: 

Well, you could record your changes to the first file. I don't know if recording will persist through a change of buffer, I suspect and let's assume not. If it's only a few dozen files I would just manually playback the recorded sequence on all of them. If you want a longer-term, less ad-hoc solution, I expect it's possible to create a vim function that cycles between buffers and does normal @a on each of them (supposing you've recorded your changes to the first buffer in register a), then does :wq and repeats on the next buffer.

In vim, do :help 10.1 and :help complex-repeat.

I recognize that this doesn't satisfy your request "and rather than editing the files for me in sequence, vim will somehow recognize that they're identical and save my changes to all files simultaneously."

profjim
+1  A: 

you can use vim on the command line. eg replace all "old" with "new" (but only for 1 file).

vim -c '%s/old/new' -c "wq" file

look up the vim docs to see if you can do it for mulitple files( :bn etc). As one of comments suggested, vim is an editor, same as sed/ed/awk. Its purpose is to edit files, therefore using *nix tools is just the same. you might want to consider writing a script to do that.

ghostdog74
+1  A: 

I would use vimdiff to edit all the README files simultaneously (showing you any differences; you can maximize the current window using CTRL-W |).

Then to save all the files, use a command like :SaveAll (only saves your arguments, not any other buffers you may have opened).

    function SaveAll()
        let i = 0
        "can't save other files while their buffers are open
        exe 'on'
        while i < argc()
            exe 'w! ' . argv(i)
            let i = i + 1
        endwhile
    endfunction

    command SaveAll call SaveAll()
rampion
Novel, but vimdiff chokes on lots of files. Still, best answer.
mbac32768
+3  A: 

If they are currently identical, you want to modify them identically, and they are to remain identical, I would do this:

Edit the first one, and then delete all the others. Then I would symlink all the others to the first one.

prestomation
This! Why edit them all if they're identical?
Christoffer Hammarström
Downvoted for failing to read.
mbac32768