2

I'm recently working on a project in vim, and I need to execute the same command to different files which are in the same folder in command-line mode multiple times. like

:%s/V1/V2/g

Is there a easiler way to do this?

  • 1
    Sounds like a better job for leaving Vim and going to terminal. Then you can use bash, sed, or awk (assuming you're on a Linux-esque terminal). There is a way to apply a command to all open buffers in Vim, but that requires you to [open all the files](https://stackoverflow.com/questions/3024321/run-a-macro-in-all-buffers-in-vim) you want to apply this to first. – wxz May 26 '22 at 15:37
  • 1
    https://vi.stackexchange.com/ – Rob May 26 '22 at 16:51

1 Answers1

3

It's command-line mode, not "command mode".

From within Vim

  1. Set your :help argument-list to the desired list of files:

    :arg /path/to/dir/*.xyz
    
  2. Perform your substitution on every file in the argument list and write it if there was a change:

    :argdo %s/V1/V2/g | update
    

See :help :arg, :help :argdo, :help :update.

From your shell

  1. Start Vim with each desired file as argument:

    $ vim /path/to/dir/*.xyz
    
  2. Perform your substitution on every file in the argument list and write it if there was a change:

    :argdo %s/V1/V2/g | update
    
romainl
  • 186,200
  • 21
  • 280
  • 313