简体   繁体   English

vim如何替换反斜杠和&

[英]vim how to replace backslash and &

I am using vim and I want to replace 我正在使用vim,我想替换
&\\\\ \\hline to \\\\ \\hline Can somebody teach me how to do this? &\\\\ \\hline\\\\ \\hline有人可以教我该怎么做吗? %s/\\&\\\\/\\\\/gc didn't work. %s/\\&\\\\/\\\\/gc无效。 this is for latex table. 这是乳胶表。

Inside the search pattern, you don't need to escape & ( \\& is a special atom for branches), but \\ needs to be doubled. 在搜索模式内,您不需要转义&\\&是分支的特殊原子),但是\\需要加倍。 In the replacement part , a & would have to be escaped, just like the \\ (yes, this is a bit complex, unfortunately). 替换部分 ,必须像\\一样对&进行转义(是的,不幸的是,这有点复杂)。 So, this would work: 因此,这将起作用:

:%s/&\\\\/\\\\/gc

If you want to assert the following \\hline , too, it's simpler to end the matching with \\ze (but still assert that the following part is there); 如果您也想声明以下\\hline ,则以\\ze结束匹配更简单(但仍然声明下面的部分在那里); this avoids having to duplicate the part that should be kept (or alternatively capture and re-insert it in the replacement): 这避免了必须复制应保留的零件(或捕获并重新插入替换零件):

:%s/&\ze\\\\ \\hline//gc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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