How do I prevent changing buffer when using the :bufdo command?

vim

Solution

You can save off the current buffer before running the command, then jump to it after:

:let buf=bufnr('%') | exec 'bufdo some_command' | exec 'b' buf

Problem

This command replaces some text in all my buffers: ``` :bufdo %s/some_text/other_text/ge | update ``` When running this command, the buffer of the current window is changed to the last buffer affected by `:bufdo`, as explained in `:help :bufdo`: ``` The last buffer (or where an error occurred) becomes the current buffer. ``` I know it's possible to prevent the buffer from being changed, but I don't remember how.

Original source