Definition of neighbours in Islamic teaching
Question

                Who constitutes as neighbours in Islam, are they only those who live closest to you or everybody in your whole neighbourhood? What are the rights of neighbours?
Praise be to Allah.Firstly:
The Islamic texts, namely the Qur’an and Sunnah, enjoin kind treatment of neighbours and affirm their rights. Allah, may He be exalted, says (interpretation of the meaning):
“Worship Allah and join none with Him in worship, and do good to parents, kinsfolk, orphans, Al-Masakin (the poor), the neighbour who is near of kin, the neighbour who is a stranger, the companion by your side, the wayfarer (you meet), and those (slaves) whom your right hands possess. Verily, Allah does not like such as are proud and boastful”
[an-Nisa’ 4:36].
Al-Bukhaari (5185) and Muslim (47) narrated from Abu Hurayrah that the Prophet (blessings and peace of Allah be upon him) said: “Whoever believes in Allah and the Last Day, let him not annoy his neighbour.”
Al-Bukhaari (6015) and Muslim (2625) narrated that Ibn ‘Umar (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said:  “Jibreel kept urging me that neighbours should be treated well until I thought he would make them heirs.”
Secondly:
The scholars differed concerning the definition of a neighbour.
Some of the scholars were of the view that the definition of neighbours according to Islam is forty houses in each direction.
Others were of the view that it is defined linguistically, and that the neighbour is only the one whose house is next to your own.
It was also suggested that the people of each mosque are neighbours, or that this applies to the people of a quarter (area of a city), or the people of a city… And there were other suggestions.
In Mughni al-Muhtaaj (4/95), it says:
Neighbours are forty houses on each side, as was stated by ash-Shaafa ‘i. That is indicated by the report: “The rights of neighbours apply to forty houses like this and like this and like this” – and he pointed to the front, to the back, to the right and to the left. Narrated by Abu Dawood and others with a mursal isnaad, and it has other isnaads that strengthen it. [The hadith was classed as da‘eef by al-Albaani. See: al-Irwa’ (6/100)]
And it was said that neighbours are those whose houses are next to yours;
or the people of the locality in which you live;
or those whose houses are next to yours or facing yours;
or the people of a dead-end street or cul-de-sac;
or those between whom and you there is clear access;
or those who pray with you in the mosque;
or your tribe;
or all the people of the city, because Allah, may He be exalted, says (interpretation of the meaning): “If the hypocrites, and those in whose hearts is a disease (evil desire for adultery, etc.), and those who spread false news among the people in Al-Madinah, cease not, We shall certainly let you overpower them, then they will not be able to stay in it as your neighbours but a little while” [al-Ahzaab 33:60]. End quote.
Some of the scholars were of the view that there is no specific definition of neighbours; rather the matter should be referred to ‘urf (custom).
Ibn ‘Aabideen (may Allah have mercy on him) said in his  Haashiyah (2/259):
The neighbour, according to custom, is the one whose house is next to yours or the one who lives in the same locality.
[The word translated here as locality refers to the place in which a tribe lives and settles; it is more akin to a small quarter or neighbourhood]
In al-Insaaf (11/256), al-Mardaawi mentioned the difference of scholarly opinion concerning that, then he said:
And it was said that this matter should be referred to custom.
I (al-Mardaawi) say: And this is the correct view, if the hadith is not saheeh. End quote.
Ibn Qudaamah (may Allah have mercy on him) said:
The instruction to treat neighbours well includes the people of forty houses on each side. This was stated by Ahmad and was also the view of al-Awzaa‘i and ash-Shaafa‘i. Abu Haneefah said: The neighbour is the one whose house is next to yours. Qataadah said: The neighbour is one or two houses.
Abu Yoosuf said: Neighbours are the people of the locality, if they pray in the same mosque. If the people of the locality are divided between two small mosques that are close to one another, then they are all neighbours, but if the two mosques are big, then the people of each mosque are neighbours to ane another. As for cities in which there are tribes, then neighbours are defined by branches of the tribes.
Then he quoted the hadith: “The rights of neighbours apply to forty houses like this and like this and like this” and said: This is a text which cannot be ignored if it is saheeh. If the report cannot be proven, then the neighbour is the one who is close by, and reference should be made concerning that to what is customary.
End quote from al-Mughni (6/233)
See also: Fath al-Baari (10/447) and al-Mawsoo‘ah al-Fiqhiyyah (16/217)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
The neighbour is the one whose house is next to your house and is close to it. There are some reports which indicate that the neighbour is forty houses on each side.
Undoubtedly the one whose house is next to yours is a neighbour. With regard to those living beyond that, if the reports which say that are soundly narrated from the Prophet (blessings and peace of Allah be upon him), then the truth is what those reports say. Otherwise, the matter should be referred to what is customary. So whatever people customarily regard as a neighbour is a neighbour.
End quote from Sharh Riyadh as-Saaliheen (3/176)
Shaykh al-Albaani (may Allah have mercy on him) said:
The scholars differed concerning the definition of neighbours, and there are several views as mentioned in al-Fath (10/367). Everything that has been narrated suggesting that the Prophet (blessings and peace of Allah be upon him) defined it as being forty (houses in each direction) is da‘eef and is not saheeh.
What appears to be the case is that it is to be defined according to what is customary.
End quote from Silsilat al-Ahaadeeth ad-Da‘eefah (1/446).