i+2 means i+2’th neighbour in a bonding pair. Getting the neighbours of the bonded pair is a non trivial task on top of that accessing those neigbours within the lammps code is even harder. There is no simple example. I have written lammps modules for deleting, updating partial charges, angles and dihedrals as soon as the bonds are formed. An excerpt from my input script looks like
fix 1r all bond/create 1 14 15 12 235 iparam 1 20 jparam 1 17 prob 1 4546 atype 1 dtype 680
Also, one has to take care of multiterm dihedral (more than one set of parameters for a given dihedral). If you want to use the set keyword to set dihedral potentials for a multiterm dihedral you have to use dihedral style fourier since the dtype keyword does not print duplicates.