The Castle of Vilar Maior (Portuguese: Castelo de Vilar Maior) is a well-preserved medieval castle located in the civil parish of Aldeia da Ribeira, Vilar Maior e Badamalos, in the municipality of Sabugal, Guarda district, Portugal.
 Discoveries in 1956 that included a sword, some rock art (consisting of geometric designs) and axes, in the Quinta da Formiga and in Fraga, suggest a period of occupation dating from the Neolithic, late Bronze Age and Roman period. At about this time, from similar examples, it is presumed that a pre-Roman castro existed on the site. Around 60 A.D. Romans conquered the Lusitanian castro, but reconstructed and refortified the site. Fragments from tiles, ceramics and coins, in addition to vestiges of mosaics and Roman pavement, were discovered in and around the former castro.
 In 1139, the settlement was taken by Leonese troops from the Moors and a castle reconstructed on the site. It eventually began to pertain to the Order of São João do Pereiro. A 1227 Latin document referred for the first time to the Vilar Maior, a toponymy that was associated with the early small colony; "vilar", which is a diminutive of "vila" (or town) referred to the site occupied by a group of one of two families.
 In 1230, the site was reconquered by Leonoses forces from Portuguese troops. On 6 August 1232, a letter was issued by King D. Alfonso IV of León to encourage settlement in the region. But this was annulled by King D. Fernando II of León, who reintegrated Vilar Maior into the municipality of Sabugal. His successor was then responsible for the reconstruction of the castle in the intervening years.
 Between 12th and 13th century, the location was easily defended by natural defenses, but with the advent of canons the site began to be considered vulnerable. By the second half of the 13th century, a new settlement letter was issued by King D. Alfonso X of Castile. By 1280, from an inscription, the urban perimeter wall ordered constructed by Alfonso X of Castile or, at least, the line that terminated at the Rua do Arco was concluded.
 In 1296, the castle was conquered by forces loyal to King D. Dinis, at the time garrisoned by Leonese D. Sancho Peres de Ledesma. A foral (charter) was conceded on 27 November 1296. The castle, or at least the keep tower, was constructed by King Dinis, with the assistance of the Knights Templar. By 12 September 1297, the territory was integrated into the Portuguese kingdom, during the sequence of the Treaty of Alcanices.
 Between 1367 and 1383 there were interventions in the castle under the initiative of D. Fernando, followed-up in 1398 by King D. John I. This work on the site, ultimately, led to the creation of the couto of Vilar Maior, which had attained a population of approximately 1000 residents by 1440. By the 1496 "Inquirição" (Inquiries), Vila Maior had reached a population of 234 residents.
 On 1 January 1510, the towns foral was upgraded and new interventions in the castle were undertaken by King D. Manuel I. Designs by Duarte d'Armas observed that the citadel included a double line of walls. Yet, even in this interpretation there were ruins and a line of walls that encircled the keep tower. The second line of walls, identifiable by a barbican, improved the defense and protection of the entryway. The keep tower was covered in tile and was accessible by a stone staircase that parted from the courtyard to the second level doorway. The design also included scaffolding around the tower, permitting a better line of protection around the keep and improving its protection. The scaffold protected the archers and created the conditions for vertical vollies identical to the machicolations, making the castle's defense more complex. Alongside the bridge were remains of town walls, therefore suggesting a third line of defenses. Today there are remnants of this group in the staircase near the old municipal building, along the Rua da Costa, and within the Museum of Vilar Maior; a majority of these walls were destroyed to make way for the walls of the cemetery.
 On 16 May 1516, the judges of Vilar Maior and Ciudad Rodrigo were responsible for recognizing the borders between the Portugal and Spain, demarcating the frontier in their decision.
 By 1527, the Cadastro do Reino (Kingdom's Cadastre), there were 60 dwellings in Vilar Maior, and a population of 422 inhabitants.
 By the 17th century, the castle had no defensive importance, resulting in reconfiguration and adoptions of the bastions, in comparison to the neighbouring defenses at the Castle of Almeida and Castle of Penamacôr. Brás Garcia de Mascarenhas wrote of the town:
 On 27 January 1653, D. Fernando Teles de Menezes Silva, was made the Count of Vilar Maior, later Marquês of Penalva and Alegrete, and Commander of the Order of Malta.
 During the events of the 1755 Lisbon earthquake, the castle was unaffected by the cataclysm.
 During references from the 1758 Memórias Paroquiais, written by Francisco Antunes, the alcalde was identified as Joaquim José Nobre de Miranda. It was defined as encountered "encircled, [but] with little defense for being in ruins", had three watchtowers, also in ruins, with tile and wood and walled castle, with little water and many buildings in ruin. The two register tower was also in a pessimistic state of conservation.
 In the 19th century, during French invasion of the peninsula, the settlement was invaded and pillaged.
 During the 1834-1853 Liberal wars, the town was also suffered the consequences of the Liberal-Miguelist conflicts.
 On 24 October 1855, the municipal status was extinguished, and the Vila Maior was integrated into the municipality of Sabugal.
 By 1864, there were 160 dwellings and the population included little more than 679 inhabitants, which progressively increased in the intervening years (by 1890, there were 199 dwellings and 742 residents). By 1940, there were 267 dwellings and only 745 inhabitants.
 In the 20th century, the former communal kiln/earthen stove was recuperated and transformed into a tourist post and handicrafts shop.
 The castle is located on the limit of the urban agglomeration, situated on a cliff plateau over the settlement dominated by the valley of the Rio Cesarão, protected in the east by the rocky cliffs of the river and west, by steep slopes. The city of Guarda is clearly visible from the heights of the plateau. In the relative proximity of the cemetery and the ruins of the Church of Nossa Senhora do Castelo; in the place of Correia, visitors can observe anthropomorphic tombs.
 The castle is Romanesque/Gothic military structure; the structure follows an oval plan, with rectangular keep tower, three registers high, straight embrasures and broken Romanesque door accessible from the battlements. The keep tower is almost addorsed to the exterior and lacks merlons along the walls, accessible by five staircases from battlements. In the western exterior section, a wall extends to the outside wall. It has two doors: in the southeast, covered with broken barrel vault, preserving monumental hinges; and in the northwest, with broken bow on the upper surface and full arch inside, covered with a broken barrel vault and preserved stonework hinges.
 There is a 35 metres (115 ft) circular cistern without covering in the interior of the courtyard.
 The 35 metres (115 ft) high, rectangular keep tower without roof extends to three floors. The northern facade has straight arrow slits on each floor, and in the east, a line of embrasures is surmounted by shell with real guns framed by protruding rectangular frame, the first coming in the second, embrasure line with half-round frame and, top, embrasure line. The south facade has an embrasure along the third register and along the western line, where the first floor is almost addorsed to the wall and second floor door with broken arch. Most of the stones are inscribed with Templar abbreviations.
 The interior was almost completely destroyed, and little remains of the visible structure of the floors, except for a flight of stairs in the north and east, and corbels supporting pavement. The battlements have a rounded profile, with vestiges of a third line of walls near the stairs that access the old hall, along Rua da Costa and Rua do Castelo and within the Museum of Vilar Maior. Remains of the third wall are also visible on the stairs leading to old town hall.
 Commemorative inscriptions on the completion of the wall, or partial segment of the urban perimeter, are visible on the wall. Recorded in the doorframe of the gate of the Rua do Arco is an inscription in capital letters, reading: