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?

2
  • 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 you want to apply this to first. Commented May 26, 2022 at 15:37
  • 1
    vi.stackexchange.com Commented May 26, 2022 at 16:51

1 Answer 1

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
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.