Explain a little differently:
:% g/found/d
Here g is an abbreviated form : g [lobal] , which has the following format:
:[range]g/pattern/[command]
And it works:
- scanning each line inside
[range] (if [range] is not specified, all lines are scanned), - marking all matches
pattern and - execution of the given
[command] .
NOTE. All occurrences of pattern matching within [range] are marked before executing [command] . Commands are executed only for marked pattern matching.
:% s/one/two/g
Here g is one of the [flags] of : s [ubstitution] of the command, which has the following format:
:[range]s[ubstitute]/pattern/string/[flags]
And it works:
- scanning each line inside
[range] (if [range] is not specified, only the current line is checked), - marking all matches
pattern , - substituting
pattern the above two conditions with string and - calling
[flags] (for example, "c" to confirm, "g" for all occurrences).
NOTE. g is the localized flag for :s[ubstitution] and is not universally used with other commands, unlike the command :g[lobal] .
Using :g[lobal] and :s[ubstitute] together
Replacing the command [command] from :g[lobal] with :s[ubstitution] , we get the following format:
:[range_g]g/pattern_g/[range_s]s/pattern_s/string_s/[flags]
By analyzing, we find out the following:
- Our world is limited to matching
pattern_g inside [range_g] . - In this limited world, we execute the command
:s[ubstitute] . - This
:s[ubstitute] is executed only if pattern_s matches inside [range_s] .
Example:
Given the following text:
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackexchange.com www.stackoverflow.com www.stackexchange.com http:
CASE 1:: :g/com/d
CASE 2:: :g/com$/d
stackoverflow.com/ stackoverflow.com/
CASE 3:: :g/com$/s/exchange/overflow/g
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackoverflow.com // changed www.stackoverflow.com www.stackoverflow.com // changed http://www.stackoverflow.com http://www.stackoverflow.com // changed
CASE 4:: :1,7g/com$/s/exchange/overflow/g
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackoverflow.com // changed www.stackoverflow.com www.stackoverflow.com // changed http://www.stackoverflow.com http://www.stackoverflow.com
CASE 5:: :1,7g/com$/4,6s/exchange/overflow/
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackoverflow.com // changed www.stackoverflow.com www.stackexchange.com http://www.stackoverflow.com http://www.stackoverflow.com
CASE 6:: :1,7g/com$/4,6s/exchange/overflow/g
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackoverflow.com // changed www.stackoverflow.com www.stackexchange.com // changed http://www.stackoverflow.com http://www.stackexchange.com
CASE 7:: :s/exchange/overflow/g
stackoverflow.com/ stackexchange.com/ stackoverflow.com stackexchange.com www.stackoverflow.com www.stackexchange.com http://www.stackoverflow.com http://www.stackoverflow.com // changed. only current line is executed