1

Let's say I have 10 files inside a folder and I open them all with vim *. All those files will be opened in the buffer. Many times when I want to edit all of them at once, I create a small macro that does the things that I want, then I save the file and go to the next one. It ends up being something like the following:

$ vi *
qa$BcwnewWord<ESC>:w:nq

By doing that, I can easily repeat this macro on the next file using @a, or, alternatively, on all files by putting a number on it like 10@a. This logic is completely functional and it works fine. However, I'm curious if there's an internal command that allows me to edit all files inside the buffer at once. Is it possible? Just like the :g allow me to execute vim commands on all lines inside a file, is there a way of executing a command on all buffered files at once?

1
  • I think you might be mistaking buffers and windows. If you open 10 files, 10 buffers will be open, but only 1 window will display each buffer one after the other. – Zorzi Apr 27 at 16:42
3

Make the macro for a single file:

qa$BcwnewWord<esc>q

and use :argdo (also :bufdo/:windo/:tabdo/:cdo/etc.)

:argdo normal! @a
:wall

If you can frame the change in an ex command, it gets to be a single step:

:argdo substitute/\w\+$/newWord/g | update
4
  • Cool, this second command was exactly what I was looking for, thanks! Just as a curiosity, is there any way of undoing the last action if something goes wrong? I realized that just trying to use u doesn't work after the changes are made... – raylight Apr 27 at 16:58
  • @raylight probably argdo undo or argdo normal! u, but I'm not 100% sure. Also what ever VCS ops can help /shrug – D. Ben Knoble Apr 27 at 16:59
  • 3
    Note that if you don't save the file at the end of the operation (in the @a macro), you will need to have :set hidden enabled to move to the next buffer without saving the current one. There's an advantage to not saving it (like you do with | update on the second approach) in that it's easier to undo changes while the buffers are modified. The :set hidden option is actually pretty handy and I think it's one that's worth considering adding to your vimrc (keeping it enabled permanently.) – filbranden Apr 28 at 5:16
  • Cool, in my case the :set hidden was the missing point to make the :argdo undo work... If I save the files, it seems that it doesn't allow me to undo changes on files that are not open on my current window anymore. – raylight Apr 28 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.