2

I just don't undstand the VIM help for pattern matching and substitution. I've tried.

I have many lines of code, like so:

G1 X139.164 Y115.348 E8.40357 ; perimeter
G1 X138.903 Y115.845 E8.57778 ; perimeter
G1 X136.919 Y119.355 E9.82896 ; perimeter
G1 X135.204 Y125.148     F250    
G1 X133.565 Y124.281 E11.75686     F250    

I want to remove uppercase E and everything following it on each line - or truncate each line, beginning with the E.

Things I've tried:

:%s/E\>//g
:1,$/E\>\.//
:%s/E\>\zs.*//

Please help.

3

Your last attempt is almost correct. I would use this:

:%s/E.*//

The OCD part of me would also want to remove any spaces before the E. That could be done with:

:%s/\s*E.*//

The \> was causing your attempts to fail because by default numbers are considered part of the word. See :help \> and :help iskeyword for more info.

The \zs isn't necessary in this case since you are trying to replace the entire search pattern. If you were to use it, you would want it in front of the E since you want to remove the E as well. The \zs would be useful if, for example, you only wanted to remove the E and everything after on lines starting with G2. Then you could do something like:

:%s/^G2.*\zs\s*E.*//

  • Thanks very much. While all three answers worked, this one helped me understand what I was doing wrong. – Davo Jun 19 '17 at 11:10
  • Glad I could help. – Pak Jun 19 '17 at 17:22
6

You can use this command:

:1,%s/E.*$//g
  • 5
    You don't need the //g at the end. Also, you can just do :% instead of :1,% – DJMcMayhem Jun 14 '17 at 20:20
  • 3
    You don't need the $ either, since * matches as much as possible (though it doesn't hurt). – Antony Jun 14 '17 at 21:35
  • I am just used to going it that way, Thanks for the comments! – Christopher Collins Jun 15 '17 at 12:28
5

@Christopher Collins' solution is great. Here's one more way you could do it:

:%norm fED

This tell vim to press fED on each line as if you had typed it in normal mode. fE will move the cursor forward to the first E, and D deletes everything until the end of the line.

  • 1
    had no idea that you could do this! This is a much better way of doing it. – Christopher Collins Jun 15 '17 at 12:30
  • 1
    @ChristopherCollins I won't say better. :%s/E.*// is neat, efficient, probably much faster, and it works with sed, and the venerable vi as well. – Luc Hermitte Jun 15 '17 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.