Neat little trick using the global ex command to remove all lines matching a given pattern.
:g/.*foo.*/d
Of course you can use any ex command at the end and you can put in whatever pattern you'd like.
Subscribe to:
Post Comments (Atom)
A blog dedicated to text editing and general exploration of computing knowledge
5 comments:
i'm really learning to love the global command. my favorite application is in conjunction with the 'normal' command. When an ordinary macro needs to be applied at numerous points in a single file, the :g command can be used to apply it or a similar sequence at multiple points.
Example:
Insert a blank comment on the line before every instance of a match in a file:
:%g/.../normal O//
if the sequence you need to perform is complex, go ahead and make a macro, and to run it in the desired spots, do :%g/.../normal @q (assuming q is the macro register). This will execute the q macro starting at any lines which match.
Important note when using :global -
:global will execute the given Ex command at any lines in the file that match the given pattern. it does NOT put the cursor at the start of the given match! this messed me up big-time at first, especially with :%g/.../normal... commands.
Awesome tip! Thanks Chris.
Thanks a lot..works like a charm!
Alternately, delete all lines NOT matching keyword:
:g!/foo/d
Casey
why doesn't write work?
I do the following:
:g/foo/w newfile
and I get all the lines in the current file.
Post a Comment