简体   繁体   English

Vim-E488错误-搜索和替换命令中的结尾字符

[英]Vim - E488 error - Trailing characters in search and replace command

The instructions say to: "Write a command that will change all "-" characters to "/" characters in every line that begins with an "F" or a "C" character." 指令说:“写一个命令,将以“ F”或“ C”字符开头的每一行中的所有“-”字符更改为“ /”字符。

I tried this in Vim but I'm getting E488: Trailing characters. 我在Vim中尝试过此方法,但得到了E488:尾随字符。 Any suggestions? 有什么建议么? Thanks. 谢谢。

:%s/^\(F|C\)/\-/\//g

The traditional way of doing do in all lines matching in Vi is to use a :g command. 在Vi 中所有匹配行中执行操作的传统方式是使用:g命令。 So in your case, this would be: 因此,在您的情况下,这将是:

g/^[FG]/s#-#/#g

Which means, do a substitute command on all lines that start with either a F or a G . 这意味着,在以FG开头的所有行上执行替代命令。 Note, since you want to use the / as replacement char, I have been using a different delimiter # . 请注意,由于您想使用/作为替换字符,因此我一直在使用不同的定界符#

If you need to use a slash in your match or replacement, it's best to use another character as your separator -- a comma will do: %s,/,-,g 如果您在比赛或替换中需要使用斜杠,则最好使用另一个字符作为分隔符-逗号会起作用: %s,/,-,g

Here, you also need to use a positive look-behind assertion: Replace any dash that's preceded by anything that begins with F or C, with a slash. 在这里,您还需要使用正向后断言:用斜杠替换以F或C开头的任何破折号。 In vim, this is written using \\@<= 在vim中,这是使用\\@<=编写的

:%s,\\(^[FC].*\\)\\@<=-,/,g

More information: :help pattern 更多信息::help模式

Solution: 解:

:g/^[FC]/s/\-/\//gc

Explanation: 说明:

^[FC]: Do replacements only in those lines which start with F or C.
\-: Look for -
\/: Replace with /
gc: Do the replacements interactively. (Change it to g for non-interactive mode).

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

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