mbac32768
mbac32768

Reputation: 11623

edit many identical files in vim and save them all with one command

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.

Upvotes: 0

Views: 261

Answers (4)

prestomation
prestomation

Reputation: 7440

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.

Upvotes: 3

rampion
rampion

Reputation: 89093

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()

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342463

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.

Upvotes: 1

dubiousjim
dubiousjim

Reputation: 4822

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."

Upvotes: 0

Related Questions