Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace in multiple files using vim

Tags:

vim

unix

Is it possible to apply the same search and replace in multiple files in vim? I'll give an example below.

I have multiple .txt files — sad1.txt until sad5.txt. To open them, I'll use vim sad* and it opened already. Now inside the 5 txt files they have similar word like happy999; I would like to change it to happy111. I am currently using this code:

argdo %s/happy999/happy111/gc | wq!

Eventually only the sad1.txt is changed. What should I do to run one script in the 5 txt files?

like image 564
Roger Sim Avatar asked May 24 '16 22:05

Roger Sim


2 Answers

Use:

:set aw
:argdo %s/happy999/happy111/g

The first line sets auto-write mode, so when you switch between files, vim will write the file if it has changed.

The second line does your global search and replace.

Note that it doesn't use wq! since that exits. If you don't want to use auto-write, then you could use:

:argdo %s/happy999/happy111/g | w

This avoids terminating vim at the end of editing the first file.

Also consider looking on vi and vim for answers to questions about vi and vim.

like image 109
Jonathan Leffler Avatar answered Sep 21 '22 20:09

Jonathan Leffler


That is a task for sed -i (-i for "in place", works only with GNU sed). Yet, if you really want to use vim or you do need the /c to confirm the replace, you can do it in two ways:

With some help from the shell:

for i in sad*.txt; do
    vim -c ':%s/happy999/happy111/gc' -c ':wq' "$i"
done

(the /c will still work, and vim will ask for each confirmation)

Or with pure VIM

vim -c ':%s/happy999/happy111/gc' -c ':w' -c ':n' \
    -c ':%s/happy999/happy111/gc' -c ':w' -c ':n' \
    -c ':%s/happy999/happy111/gc' -c ':w' -c ':n' \
    -c ':%s/happy999/happy111/gc' -c ':w' -c ':n' \
    -c ':%s/happy999/happy111/gc' -c ':wq' sad*.txt

(In my humble opinion this last one looks horrible and repetitive and has no real advantages over the shell for, but it shows that pure vim can do it)

like image 31
grochmal Avatar answered Sep 19 '22 20:09

grochmal