简体   繁体   中英

Regex recursive replace in vim when pattern has tilde

My initial file is

192.168.210.12
192.168.210.13
192.168.210.14
192.168.210.15
192.168.210.16
192.168.210.17
192.168.210.18
192.168.210.18

Pattern for replance is %s/\\(192.168.210.[0-9]\\+\\)/\\"ssh\\ myuser@\\1\\ -i\\ ~\\/Documents\\/my_key\\"/g

After running above command, I got the following file.

"ssh myuser@192.168.210.12 -i /Documents/my_key"
"ssh myuser@192.168.210.13 -i /Documents/my_key"
"ssh myuser@192.168.210.14 -i /Documents/my_key"
"ssh myuser@192.168.210.15 -i /Documents/my_key"
"ssh myuser@192.168.210.16 -i /Documents/my_key"
"ssh myuser@192.168.210.17 -i /Documents/my_key"
"ssh myuser@192.168.210.18 -i /Documents/my_key"
"ssh myuser@192.168.210.18 -i /Documents/my_key"

After calling :u and then same replace, I got the following:

"ssh myuser@192.168.210.12 -i "ssh myuser@192.168.210.12 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.13 -i "ssh myuser@192.168.210.13 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.14 -i "ssh myuser@192.168.210.14 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.15 -i "ssh myuser@192.168.210.15 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.16 -i "ssh myuser@192.168.210.16 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.17 -i "ssh myuser@192.168.210.17 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i /Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i /Documents/my_key"/Documents/my_key"

Again, I call :u and replace, it gives following output

"ssh myuser@192.168.210.12 -i "ssh myuser@192.168.210.12 -i "ssh myuser@192.168.210.12 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.13 -i "ssh myuser@192.168.210.13 -i "ssh myuser@192.168.210.13 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.14 -i "ssh myuser@192.168.210.14 -i "ssh myuser@192.168.210.14 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.15 -i "ssh myuser@192.168.210.15 -i "ssh myuser@192.168.210.15 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.16 -i "ssh myuser@192.168.210.16 -i "ssh myuser@192.168.210.16 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.17 -i "ssh myuser@192.168.210.17 -i "ssh myuser@192.168.210.17 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"
"ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i "ssh myuser@192.168.210.18 -i /Documents/my_key"/Documents/my_key"/Documents/my_key"

However, when I remove ~ from the pattern (ie use this %s/\\(192.168.210.[0-9]\\+\\)/\\"ssh\\ myuser@\\1\\ -i\\ \\/Documents\\/my_key\\"/g ), then it behaves expected.

What is the reason for this unusual replace behaviour on ~ .

From :h :s

magic   nomagic   action   
  ~   \~      replaced with the {string} of the previous substitute  s~
 \~    ~      replaced with ~                                         s/\~

Applied to your substitute expression, the ~ gets replaced with the string of the previous substitute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM