What is the difference between :g and :%s commands in vim - vim

today I started to use vim. I get confused at :g and :%s commands. So, what is the difference between :g or :%s commands?

:g, short for global, executes a command on all lines that match a regex:
:g/LinesThatMatchThisRegex/ExecuteThisCommand
Example:
:g/hello/d
This will delete (d) all lines that contain hello.
On the other hand, :%s just performs a search (on a regex) and replace throughout the file:
:%s/hello/world/g
The g at the end means global or greedy (this is disputed) so it will replace all occurrences on the line, not just one per line. You can also use the c flag (:%s/hello/world/gc) if you want to confirm each replacement manually.
This command replaces all occurrences of hello with world.
Both the :g and :%s commands support regular expressions.
The s command means substitute and the % means throughout the buffer. So %s means substitute throughout the entire buffer. You can also give a line range:
:10,15s/hello/world/g
This will execute the search and replace seen earlier on only lines 10 to 15 (inclusive).

They are different.
:g can execute commands for matched lines. :s is one of those commands. That is you can combine :g and s
:%s just do search and replace on whole buffer, even though it can do some other things with expression too, but it is not as straightforward as :g.
E.g.:
:g/foo/s/bar/blah/g
this will do bar->blah substitution on lines which contain foo. With :s we could:
:%s/foo/\=substitute(getline('.'), 'bar','blah','g')
so :g is easier.
So if you are dealing with substitution task, usually :s should come up first. If you want to do something like for all lines that matches xxx, I want to delete/join/indent/....... :g maybe helpful for you.

Review:
The ":" mode (e.g. ex-mode) commands in vi or vim have this form:
[Address-specifier] [command] [command-specifics] [cmd-modifiers]
Address can be a single line address (ex-mode operates on "lines"), or a line range.
For instance, a very simple command in "p" which will print the addressed line(s).
:1p - will print line 1.
:5p - will print line 5.
:1,5p - will print lines 1 through 5. 1,5 is an address range.
:7,+3p - will print lines 7 through 10 (7,7+3=10). A relative range.
There are some shorthands in the address space. $, and % are the most popular.
$ means "last line in the file". Thus the expression:
1,$p - will print all lines, 1 to the LAST-line in the file.
The expression 1,$ is so frequently used (e.g. apply the following command to all lines in the file) that it has an even shorter, shorthand, %. % means "1,$"
So:
%p - will print all lines, 1 to the LAST-line in the file, just like 1,$
There is also a special "global" command, whose effect is to supply a set of address prefixing, that is not necessarily a linear range of lines, but is instead determined by a a regular expression match. The ":g/regex/" prefix fits into the "Address specifier" part of the ex-command format (not the command part, which follows it).
It allows specifying a "list" of lines, matched by regular expression rather than "line number", or "range of lines". The matching applies by the regular expression showing up in the line, and then that line is include in the list of lines to which the command will apply.
Application of :1,$s vs %s vs :g/./s
Using the following file as an example:
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
This command, using the global prefix/regex for address, and the "p" print command:
:g/1 2/p - will print
2: 1 2 3 4 5 6 1 2
4: 2 3 1 2
Line 2, and 4 both matched the :g/1 2/ regular expression, and expands effectively into a list of line numbers, with the following command applied to each item in the list. Approximately like this command).
:2p 4p
The substitution command allows substituting a field matching a regular expression, with other text. If we applied the substitution command to our example file, on line 2, we can see its effect.
1: ....
2: 1 2 3 4 5 6 1 2
3: ....
Command:
:2s/1 2/2 1/ will change line 2 to be instead: 2: 2 1 3 4 5 6 1 2
It changes ONLY the first instance of the pattern "1 2" to be "2 1".
If we "undo" this command using "u", we can then run the command again, modified.
We can use the "p" modifier on the command, which for "substitute" does not do much.
It applies the change, but also prints the applied changes at the bottom of the screen (somewhat redundantly in this example).
:2s/1 2/2 1/p
u (to undo), and then we try it again.
We can use the "c" modifier to ask for confirmation.
:2s/1 2/2 1/c The "confirm" modifier for the substitute asks for confirmation on each change.
u (to undo).
The "global" modifier. (Not the global address/regex address operator) can make the substitute command perform multiple substitutions on a line.
:2s/1 2/2 1/g - The "g" here is a modifier to the "s" substitute command.
It means perform the the substitution globally on THE LINE. Modifiers modify "commands", and commands apply to 1 or more lines, as set the address field. The "g" applied to the end of the substitute command means: substitute globally on this line, e.g. every time the regular expression of the substitute command occurs, perform the substitute.
2: 2 1 3 4 5 6 2 1 - Here, both the first and second instance are substituted.
If the substitute command cannot find its match regular expression, then it does nothing. This means it can be applied to a range of lines, and have impacts only on the lines that have at least one match to the substitute command regex.
1,4 s/1 2/2 1/
1,$ s/1 2/2 1/
%s/1 2/2 1/
are all equivalent, and will substitute the FIRST occurrence of the
substitute commands regex match pattern, with the substitute pattern.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
becomes:
1: 1
2: 2 1 3 4 5 6 1 2
3: 3 2 1
4: 2 3 2 1
Adding "g" to the end gives:
:%s/1 2/2 1/g
1: 1
2: 2 1 3 4 5 6 2 1
3: 3 2 1
4: 2 3 2 1
The g:/regex Prefix
The :g/regex/ Address specifier applies to any command that follows it, and that command can include the substitute command, including with the "g" modifier.
:g/3 4/s/1 2/2 1/g
This command says, "globally match lines with regex /3 4/" and then run command
:s/1 2/2 1/g.
Only line 2 includes the regex /3 4/, so only line 2 is matched. Thus on this file:
:g/3 4/s/1 2/2 1/g is equivalent to:
:2s/1 2/3 4/g, which substitutes all occurrences of 1 2 with 2 1.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
becomes:
1: 1
2: 2 1 3 4 5 6 2 1
3: 3 2 1
4: 2 3 1 2
Notices that line 4: is unchanged, because it did not have the pattern "3 4" for the Address specifier line match.
:g/regex-line-match/s/match-regex-substitute/sub-pattern/g
:%s/match-regex-substitute/sub-pattern/g
The two lines often can be equivalent in EFFECT. They can often not be equivalent. The equivalence depends on the regex patterns and their matching, and because "substitute" does nothing when a line has no matching match-regex-substitute match pattern.
% = 1,$ which matches all lines, and then applies the substitute pattern.
:g/./ would match every line, if prefixed.
The regex pattern of the "global/regex" prefix if the same as the match-pattern of the substitute would be a lot of extra typing, but would restrict the substitute command to only lines that matched the global/regex. If the global/regex expression truly match every line, such as :g/^.$/, then the global line would have the same effect as %. (Since % would match all lines, and since :g/^.$/ would match all lines, then the "s" would do the same thing in base cases. When using a more typical regular express (that matched some specific string), the :g/regex/ prefix would be different than %. The command "s" would only be applied to lines that first matched the g:/refex/ prefix, instead of to all lines 1,$. The substitute would then try and apply its own "per line" match pattern successfully (and substitute), or find no match on the given line and do nothing.
The place where the global/regex prefix is interesting, is when the global/regex prefix regular expression is different than the substitution match regex pattern. In this case, you apply global/regex FIRST (to determine which lines will then be subject to), the substitute "match-replace-regex" pattern in the substitute command (which can be different). As shown in our example above where we used a global/regex prefix of "3 4", and a substitute match-regex-pattern of "1 2", which is applied SECOND.
VERY ADVANCED:
While global/regex essentially builds a list of lines on which to apply commands, the manner in which that list is built is not the same as the 1,$ or other fixed range specifiers are. Fixed specifiers, are computed, "all at once", at the moment the :[address]command is typed. The global/regex command on the other hand, recomputes its line target after each individual application of its subordinate command.
We will use the "join" command to illustrate the difference.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
If I specify a range of commands to apply the "join" command to, using range syntax, such as: :1,$j (or :%j) would render:
1: 1 1 2 3 4 5 6 1 2 3 2 1 2 3 1 2
This happens happens because 1,$ selects lines 1,4 at the start, and then applies "j" to every line selected, combining all of the lines of the range.)
But if we instead used the global prefix operator (matching all lines), the application is different:
:g/./j
This will render:
1: 1 1 2 3 4 5 6 1 2
2: 3 2 1 2 3 1 2
The difference occurs because of "how" and "when" the command is applied in each of the two syntax. In the first :%j syntax, all the lines are computed up front, and then "j" is applied to each of those lines.
With the global/regex syntax, the lines and commands are applied on an "as you go", and "from where you are" basis, after EACH application of the command. So the :g/./j command will match LINE1 first, and then runs "j" combining lines 1+2= new-1. It then advances to the "next" line in the file (the new file, new-2), matches that line (/./ matches all) and applies "j" to new-2 (original line3), and new-3 (original 4) to create new-new-2 = 3+4. And then advances to the next line in the "new new file" which is line 3 (but there is no new-new-3, so it stops.) The result is:
1: 1 1 2 3 4 5 6 1 2
2: 3 2 1 2 3 1 2
The key difference is that after application of an instance of the command, the global regex search resumes on the "next" line of the file in existence after the application of the command.
As an earlier poster summed up in far fewer words (but assuming much more knowledge in the reader):
:g/first-search-pattern/s/match-pattern/substitute-pattern/g or /gc for confirm.
SUMMARY:
All of these patterns can be different, the trailing g or gc can be present (all occurences on each line, with or without confirm), or ommitted, (first occurence on each line only). While writing:
:%s/pattern/replace/g is common, the following is nearly equivalent:
:g/./s/pattern/replace/g (less common, but basically the with "substitute" command).

Related

How to combine and negate these two patterns together?

In VIM, I want to delete any lines that are not 2 or 3 characters.
:g/^..$/d
:g/^...$/d
Those delete 2 or 3 character lines. How to combine the two into one and negate it, namely 'don't delete 2 or 3 character lines'
You can use :v to execute a command on lines that do not match a pattern.
This requires that you use a single pattern though... Which in your case you can easily do by using the \= modifier to optionally match the last item.
So to delete all lines with either 2 or 3 characters, you can use:
:g/^...\=$/d
And to delete all lines except those with either 2 or 3 characters:
:v/^...\=$/d
Following would be my regex of choice
:v/\v^.{2,3}$/d
Try it online!
Options: Case insensitive
Assert position at the beginning of the string ^
Match any single character .{2,3}
Between 2 and 3 times, as many times as possible, giving back as needed (greedy) {2,3}
Assert position at the very end of the string $
How about "delete all lines with less than two or more than three characters"?
:g/^.\{,1}$\|^.\{4,}/d

Why is the author using a double colon in this vim command?

I'm doing a Vimgolf problem to transform this:
- One number per line -
-----------------------
2,3,5,7,
11,13,17,
19,23,29,
to this:
2
3
5
7
11
13
17
19
23
29
One solution is
::%s/,/\r/g|v/\d/d<CR>ZZ
I understand most of this command but I have a few questions:
1) Why is there an extra colon : in front of :%s?
2) What is |v/\d/d doing?
1. Colon
This is more of a typo. It doesn't mean anything but it still works the same as single colon
2. v command:
v/\d/d
This is just the second command :vglobal which will remove all the lines /d which don't contain a digit \d. It's a negation of g command - :global

vim syntax: match all lines with a given pattern that follow a line with a different pattern

I'm writing a Syntax file for Robot Framework. I want to create a color scheme for the [Documentation] tag, such that the text following the tag, and all subsequent lines beginning with ... until the next line that does not begin with ... are matched. So for example:
1 Test Case 1
2 [Documentation] This
3 ... Is
4 ... Documentation
5 [Setup] Run Keywords Keyword1
6 ... AND Keyword2
I want to match lines 2-4, but not 1, 5, or 6. I'm not sure if this is possible, and if so, I have no idea where to start.

Multilevel parsing using shell command

I have a file in the following format
/////
name 1
start_occurrence:
occurrence 1
occurrence 2
///
name 2
start_occurance:
occurrence 1
occurrence 2
///
name 3
start_occurrence:
occurrence 1
occurrence 2
occurrence 3
All I need is to make a count of the number of occurrences for each name and save them in a CSV file. Can I do it using any combination of shell commands? Yes I can do it programmatically, but looking for a bunch of shell commands in a pipe lined fashion.
"names" can be anything. Names does not come with a pattern. Only catch is that the line after /// is the name. Also Occurrence does not have any number with it, anyline that starts with occurrence or have occurrence is a subject of interest.
awk 'c=="THISISNAME"{b=$0;c="";}$1=="///"{c="THISISNAME"}$0~/\<occurrence\>/{a[b]+=1;}END{for (i in a){print i" "a[i]}}' YOUR_FILE_HERE
explain:
if match the name start condition ($1=="///"), mark the c to THISISNAME.
if this is the name line (c=="THISISNAME"), mark the name line with b, and mark c as name part ended(c="").
if match the occurrence condition ($0~/\<occurrence\>/), make a[b] += 1.
use a map a to remark the occurrence time of each name.
awk use EREs, the $0~/EREs/ means $0 match the regex. the '\<' and '>' means '\b' in PREs

Vim: Replace n with n+1

How do I replace every number n that matches a certain pattern with n+1? E.g. I want to replace all numbers in a line that are in brackets with the value+1.
1 2 <3> 4 <5> 6 7 <8> <9> <10> 11 12
should become
1 2 <4> 4 <6> 6 7 <9> <10> <11> 11 12
%s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
By way of explanation:
%s " replace command
"""""
< " prefix
\zs " start of the match
\d\+ " match numbers
\ze " end of the match
> " suffix
"""""
\= " replace the match part with the following expression
(
submatch(0) " the match part
+1 " add one
)
"""""
g " replace all numbers, not only the first one
Edit:
If you only want to replace in specific line, move your cursor on that line, and execute
s/<\zs\d\+\ze>/\=(submatch(0)+1)/g
or use
LINENUMs/<\zs\d\+\ze>/\=(submatch(0)+1)/g
(replace LINENUM with the actual line number, eg. 13)
In vim you can increment (decrement) the numeric digit on or after the cursor by pressing
NUMBER<ctrl-a> to add NUMBER to the digit
(NUMBER<ctrl-x> to substract NUMBER from the digit)
If only incrementing (decrementing) by one you don't need to specify NUMBER. In your case I would use a simple macro for this:
qaf<<ctrl-a>q
100<altgr-q>a
Here a brief explanation of the macro: It uses the find (f) commant to place the cursor on the opening < bracket. It is not necessary to position the cursor on the digit. When press the number on the cursor or the nearest number after the cursor will get incremented.
If you want an even shorter series of commands you can position your curser ONCE by pressing f<, increment the number with ctrl-a and then just repeatedly press ;.. The ; command repeats the last cursor movement i.e. the find command. The . command repeats the last text changing command.
Check out this link for further information or use the built in documentation: h: ctrl-a.

Resources