Our teacher, the Rema, wrote at the end of this siman (Shulchan Aruch Yoreh Deah 245): A rav who dwells in a city and teaches the public—another sage may come and teach there as well, even if he slightly diminishes the livelihood of the first. For example, if the community accepted the first as a rav and he receives a stipend from them for this—even so, the second may come to dwell there and maintain a rabbinate in every matter like the first, if he is great and fit for it. However, if a visiting sage comes to the city—he must not diminish the wages of the rav dwelling there, to perform weddings and betrothals and take the wages resulting from them, since it is the stipend of the rav dwelling there. But it is permitted to perform the wedding and give the wages to the permanent rav. Similarly, it is permitted for him to judge between two litigants in the city who come before him to judge, for perhaps the rav in the city is not acceptable to them. But he must not rule on matters of issur v'heter, or preach to exercise authority in the place of his fellow. And one who was established as the rav in a city, even if he established himself in some authority—he is not to be removed from his greatness even though another who is greater than him arrived there. Even his son and his grandson—they always precede others as long as they fill the place of their fathers in fear [of Heaven], and they are somewhat wise. And in a place where there is a custom to accept a rav for a fixed time, or where the custom is to choose whomever they wish—the authority is in their hands. But as long as the community accepted him upon themselves, and certainly if they did so by the will of the government—no great person in the world may exercise authority over him and remove him. Thus far his words.
And the giants of the world have already written that now the custom is that as long as there is a rav in the city—it is forbidden for another to come to the city and settle there, and it is a trespass of boundaries and absolute theft. For in truth the matter is so, that in ancient times the custom was not that every city chooses a rav for itself, but rather when a sage came and disseminated Torah—it followed automatically that he had a share of honor and some stipend. And afterward a second came and conducted himself likewise, as is evident from the books of responsa of the Ribash, the Tashbetz, the Maharik, and the Terumat HaDeshen. Therefore, they said regarding this as well that his son precedes [others]. But now that we have not seen this custom in our days, but rather the people of the city choose for themselves a certain rav to rule and to judge, and even though they make a contract with him for such and such years—nevertheless, they never remove him. And such is the simple custom in all the dispersions of Israel. And specifically if he uproots his residence from another city to here—it is certainly and certainly so that anyone who comes to trespass his boundary is an absolute thief, and he is no less than a standard employee. And in such a case, it appears to my humble opinion that the son also does not precede, even if he fills his place; for they desired to hire his father, and they do not desire the son. It is only as a matter of course, that if he fills his place in Torah and fear [of Heaven]—why should he be less than another? And such is the primary ruling.
כתב רבינו הרמ"א בסוף סימן זה (שולחן ערוך יורה דעה רמה): רב היושב בעיר ולומד לרבים – יכול חכם אחר לבוא וללמוד גם כן שם, אפילו מקפח קצת פרנסת הראשון. כגון שהקהל קבלו עליהם הראשון לרב, ונוטל פרס מהם על זה – אפילו הכי יכול השני לבוא לדור שם ולהחזיק רבנות בכל דבר כמו הראשון, אם הוא גדול וראוי לכך. אבל אם בא חכם אכסניא לעיר – אין לו לקפח שכר הרב הדר שם, לעשות חופות וקידושין וליטול השכר הבא מהם, הואיל והוא פרס הרב הדר שם. אבל מותר לעשות החופה וליתן השכר להרב הקבוע. וכן מותר לו לדון בין שני בעלי דינים שבעיר הבאים לפניו לדון, דשמא הרב שבעיר אין ממוצע להם. אבל אין לו להורות איסור והיתר, או לדרוש לנהוג שררא באתרא דחבריה. ומי שהוחזק לרב בעיר, אפילו החזיק בעצמו באיזה שררה – אין להורידו מגדולתו אף על פי שבא לשם אחר שגדול ממנו. אפילו בנו ובן בנו – לעולם קודמים לאחרים כל זמן שממלאים מקום אבותיהם ביראה, והם חכמים קצת. ובמקום שיש מנהג לקבל רב על זמן קצוב, או שמנהג לבחור במי שירצו – הרשות בידם. אבל כל שקבלו הקהל עליהם, וכל שכן אם עשו ברצון השררה – אין לשום גדול בעולם להשתרר עליו ולהורידו. עד כאן לשונו. וכבר כתבו גדולי עולם שעכשיו המנהג שכל שיש רב בעיר – אסור לאחר לבוא בעיר ולהתיישב שמה, והיא הסגת גבול וגזל גמור. דבאמת העניין כן הוא, דבזמן הקדמון לא היה המנהג שכל עיר בוחרת לה רב, אלא כשבא חכם והרביץ תורה – ממילא שהיה לו חלוקת כבוד ואיזה פרס. ואחר כך בא השני והנהיג גם כן כן, כדמוכח מספרי שאלות ותשובות הריב"ש, והתשב"ץ, ומהרי"ק, ותרומת הדשן. ולכן אמרו גם בזה שבנו קודם. אבל עכשיו שלא ראינו מימינו מנהג זה, אלא אנשי העיר בוחרים להם רב פלוני להורות ולדון, ואף שעושים עמו כתב על כך וכך שנים – מכל מקום אין מסלקין אותו לעולם. וכן המנהג הפשוט בכל תפוצות ישראל. ובפרט אם עוקר דירתו מעיר אחרת לפה – פשיטא ופשיטא שכל הבא להשיג גבולו הוה גזלן גמור, ולא גרע משכיר דעלמא. ובכהאי גוונא נראה לעניות דעתי דגם אין הבן קודם, אפילו ממלא מקומו; דאת אביו רצו לשכור, ואת הבן אינם רוצים. ורק ממילא, דאם ממלא מקומו בתורה ויראה – למה יגרע מאחר? וכן עיקר לדינא.