share
Unix & LinuxUsing regular expression with find&replace in vim
[+1] [2] nullnv0id
[2020-04-17 21:59:36]
[ find regular-expression vim directory replace ]
[ https://unix.stackexchange.com/questions/580816/using-regular-expression-with-findreplace-in-vim ]

I am wondering how to use regular expression to specify directories to be found and replaced inside vim.

For example: Changing the default DocumentRoot and Directory in httpd.conf.

Normally I use the following command to find and replace individual strings in vim, but this does not work for directories

:%s/tobefound/tobereplaced/g 
[+3] [2020-04-17 22:18:25] Andy Dalton [ACCEPTED]

Suppose that you had:

DocumentRoot /var/www/html

And you wanted to change /var/www/html to /home/web, you could do:

:%s,/var/www/html,/home/web,g

There's nothing "special" about /, it's just a common field delimiter. You can use other characters; in my example I used ,


This works best for my needs. Appreciate it Andy. - nullnv0id
The perfect answer, no escaping and no extra chars to write. I discovered it recently and had a big laugh doing it for so long so complicated. - Cellcore
1
[+2] [2020-04-17 22:05:04] Torin

Any / characters in both tobefound and tobereplaced will need to be escaped to \/, otherwise it will be interpreted as part of the syntax of the find and replace command.

E.g. replacing /tmp/abc/ with /srv/def/ would require %s/\/tmp\/abc\//\/srv\/def\//g


Thank you for taking the time to comment, I tried this and it works well - nullnv0id
2