Consider these two vim commands
:% s/one/two/g
:% g/found/d
I am wondering why the g
signaling global replacement and delete respectively needs to be put in the end in substitution and at the beginning in delete.
Do these follow a pattern that I am missing, or is this a vim corner case?
I guess you are confused with :g[lobal]
, :s[ubstitute]
and :[range]delete
let's make some example:
g
is a flag of :s
:
:s/foo/bar/
: replace only the first foo
with bar
in current line:s/foo/bar/g
: replace all foo
with bar
in current line%:s/foo/bar/
: for each line in the whole file, replace only the first foo
with bar
%:s/foo/bar/g
: replace all foo
with bar
in whole file (all lines) :g
as :global
command: :g
can do with any commands, not only d
:g/foo/d
: delete all matched lines default range is %
:%g/foo/d
: same as above:1,30g/foo/d
: from line 1-30, remove all lines contain foo
:g/foo/normal >>
: indent all lines, which match foo
(not only work with d
):g/foo/y A
: yank all matched (/foo/
) lines to register a
(not only work with d
):d
command: (:[range]d[elete]
)
:/foo/d
: /foo/
here is a range. delete next matched line:%d
: delete all lines (empty the file):%/foo/d
: this won't work. because you have two ranges (% and /foo/):/foo/dg
: this won't work either. no dg
command:g/foo/d
: this works, same as above(the :g
section), but it is from :global
commandI hope you get a bit clear. (or more confused? I hope not.. ^_^)
you may want to take a look followings
:h :s
:h :g
:h :d
:h range
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With