Reputation: 30983
I'm learning the power of g and want to delete all lines containing an expression, to the end of the sentence (marked by a period). Like so:
There was a little sheep. The sheep was black. There was another sheep.
(Run command to find all sentences like There was
and delete to the next period).
The sheep was black.
I've tried:
:g/There was/d\/\.
in an attempt to "delete forward until the next period" but I get a trailing characters
error. :g/There was/df.
but get a df. is not an editor command error.
Any thoughts?
Upvotes: 3
Views: 4879
Reputation: 1114
The action associated with g
must be able to act on the line without needing position information from the pattern match that g
implies. In the command you are using, the delete forward command needs a starting position that is not being provided.
The problem is that g
only indicates a line match, not a specific character position for it's pattern match. I did the following and it did what I think you want:
:g/There was/s/There was[^.]*[.]//
This found lines that matched the pattern There was
, and performed a substitution of the regular expression There was[^.]*[.]
with the empty string.
This is equivalent to:
:1,$s/There was[^.]*[.]//g
I'm not sure what the g
is getting you in your use case, except the automatic application to the entire file line range (same as 1,$
or %
). The g
in this latter example has to do with applying the substitution to all patterns on the same line, not with the range of lines affected by the substitution command.
Upvotes: 4
Reputation: 31040
You can use :norm
like this:
:g/There was/norm 0weldf.
This finds lines with "There was" then executes the normal commands 0weldf.
.
0
: go to beginning of line
w
: go to next word (in this case, "was")
e
: go the end of the word (so cursor is on the 's' of "was")
l
: move one character to the right (so we don't delete any of "was")
df.
: delete until the next '.', inclusive.
If you want to keep the period use dt.
instead of df.
.
If you don't want to delete from the beginning of the line and instead want to do sentences, the :%s
command is probably more appropriate here. (e.g. :%s/\(There was\)[^.]*\./\1/g
or %s/\(There was\)[^.]*\./\1./g
if you want to keep the period at the end of the sentence.
Upvotes: 3
Reputation: 392833
I'd just use a regex:
%s/There was\_.\{-}\.\s\?//ge
Note how \_.
allows for cross-line sentences
Upvotes: 3