Search code examples
vimediting

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.


Solution

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