I know a couple who worked in Gibraltar but lived in Spain. Firstly, you have to take out Spanish residency (register on the list of EU foreigners residing in Spain) and prove you have sufficient funds to live on. No problem if you have a job in Gibraltar. You also have to prove medical care which would normally mean taking out private health insurance.
However, Gib and Spain have an agreement and you can transfer your social security payments from your job in Gibraltar to the are you are living in in Spain and this will give you access to the Spanish healthcare system. You still have to pay income tax in Gibraltar.
They said they had to go to the social security office in Gib and fill in the forms to transfer the payments across and then to the social security office in Spain to accept the payments.
Once this is done you can register for healthcare and will be allocated a GP in Spain.
Whilst all this is going on, you can buy the items over the counter at a pharmacy in Spain which, depending on the item, may work out cheaper than UK. If you take the repeat part of the prescription to the pharmacy they will sell you the equivalent items but at full cost. Once registered with the social security, you will get the reduction on the cost depending on your age and income but generally 40% of the cost of the item.