Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
ArcticZombie
Sep 15, 2010
Can anyone explain this command to me? I know what the result is but I don't know what it's actually doing because all I did was modify a different thing. It finds all instances of <p> and gives them an id attribute with sequential numbers.

code:
let n=1 | g/<p>/s/<p>/\='<p id="'.n.'">'/ | let n=n+1
Here's what I can work out:

let n=1 -- Simple enough, it's defining a variable.

g/<p>/ -- From what I can tell, this searches for the pattern and collates all lines that match into a new buffer of sorts which is kind of like a more/less thing?

s/<p>/ -- Search for this pattern.

\='<p id="'.n.'">'/ -- I get that this is the replacement part of the s command and I can see the different parts of it but it starts with an escaped equals sign, has quotes surrounding it and the variable part and some periods around the variable too?

let n=n+1 -- Increasing the variable by 1. If this command is being run over and over I don't see why this part is repeated to increase the variable but let n=1 isn't repeated. If it's all repeated then n would be set to 1 at the start, the replacement would be carried out with n as 1, it would be set to n+1 (2) but then set back to 1 again? This clearly isn't the case.

Adbot
ADBOT LOVES YOU

ArcticZombie
Sep 15, 2010
Thanks fellas. I don't often use vim, in fact I only used it in this instance because of the ability to set a variable and increment it like that which you can't do with just plain regex in other editors.

  • Locked thread