How do i use vi to substitute an expression that is part of a path?

Let say I have /home/user1/bin/ and i want it to be /root/bin
I tried :

s/\/home\/user1/user2/

but it can't find the pattern user1.
I tried:

s/*\/home\/user1*/user2/

as well same result

many thanks

$
$ X="/home/user1/bin/"
$
$ echo $X
/home/user1/bin/
$
$ echo $X | sed -e 's/\/home\/user1/\/root/' -e 's/\/$//'
/root/bin
$
$

tyler_durden

---------- Post updated 06-23-10 at 02:07 PM ---------- Previous update was 06-22-10 at 03:51 PM ----------

Sorry, I did not notice that you want to do this in vi editor.

Place your cursor on the line that has the string "/home/user1/bin/" and hit ":" for the editor prompt. Then type the following and press "Enter" -

s/\/home\/user1\/\(.*\)\//\/root\/\1/

I don't have vi, but this works in vim.

HTH,
tyler_durden

1 Like

put cursor on the line you want to change and type

:%s=/home/user1/bin/=/root/bin

## if you want to globally change
:%s=/home/user1/bin/=/root/bin=g
1 Like

when you use / as delimiter then you have to properly escape the / that occurs in your pattern, or else use some other delimiter other than /.

1 Like

in the vi mode, type : then the below line. If you have multiple lines with usr/home which needs to be changed, use g at the end else just skip it.
this should work fine.

<:%s/\/usr\/home/\/root/g>

1 Like