简体   繁体   中英

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."

I tried this in Vim but I'm getting E488: Trailing characters. 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. 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 . 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

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. In vim, this is written using \\@<=

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

More information: :help pattern

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).

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