Hoàng Kế Viêm (1820–1909) was a Vietnamese General and a Dong'ge Grand Secretariat during the Nguyễn dynasty. He played a significant role in suppressing borderlands banditry and resisting French invasion during the second half of the 19th century.

Early years Hoàng Kế Viêm (1820–1909) was a son-in-law of emperor Minh Mạng. His father is Hoàng Kim Xán, the governor of province Khánh Hòa. In the year 1841, Emperor Minh Mạng died, and his eldest son Thiệu Trị succeeded to the throne.

In 1843, Hoàng Kế Viêm married Nguyễn Phúc Quang Tĩnh, the fifth daughter of Emperor Minh Mạng and thus became a Prince (Phò mã). The next year, Nguyễn Phúc Quang Tĩnh died at age 28 during pregnancy. Emperor Thiệu Trị gave her the title of Princess Hương La (Hương La Công Chúa).

Military career Anti-Imperial Bandits Yanling Kingdom In 1849, Taiping Rebellion broke out by Hong Xiuquan, together with Yang Xiuqing, Xiao Chaogui and Li Xiucheng. They later took control of Jinling and provinces south of the Yangtze River. In 1857, Liu Yongfu, the future founder of Black Flags, joined Wu Lingyun (Wu Yuanqing) who led uprisings and rebellions against Qing dynasty in Guangxi province. In 1861, Wu Lingyun formed the Kingdom of Yangling in southern Guangxi. Unlike millenarian Christianity of Taiping, Yanling kingdom did not ask his followers to choose a religious agenda. According to Trần Trọng Kim, Wu Lingyun's son Wu Kun (Wu Yazhong) was the follower of Taiping Heavenly Rebellions, while Henry McAleavy described Wu Lingyun (Wu Yuanqing) as “freebooter” taking advantage of Taiping Heavenly's prestige and absence in Guangxi after Taiping Rebellion established itself in Nanking as a dynasty. Bradley C. Davis recently further explained that Yanling kingdom and Taiping Heavenly Kingdom were two separate movements.

In 1863, Wu Lingyun (Wu Yuanqing) died when having battle with Guangxi provincial army led by Feng Zicai, which pushed Yanling kingdom into crisis. His son Wu Yazhong (also called Wu Kun) became the new leader and led his loyalists to occupied Guishun, a city in the far west of Guangxi. During this time, Wu Yazhong kept seeking followers, while Liu Yongfu decided to leave China for Vietnam with his followers whom he gathered and formed Black Flags.

White Flags and Black Flags Before Liu Yongfu's arrival, White Flags, a bandit from southern China, has been raided in northern Vietnam for many years. Another saying is, White Flags was also the former follower of Wu Lingyun. In 1866, Huế court assigned Nguyễn Bá Nghi to negotiate the surrender of White Flags. Nguyễn Bá Nghi disagreed with the idea of giving the bandit leader a position in order to make use of their strength, insisting on conventional way by strengthening military power, but he failed.

Planning to confront White Flags, Liu Yongfu formed an alliance with Deng Wan, another bandit from southern China. In February 1868, White Flags took control of Lục Yên. Liu Yongfu, who just arrived in Vietnam, was planning to lay siege to Lục Yên. In March, Black Flags captured Lục Yên. The leader of White Flags, Bàn Văn Nhị, was decapitated. Liu Yongfu thus received official position from Vietnamese authorities.

Wu Yazhong and Yellow Flags In pursuit of Liu Yongfu, Wu Yazhong entered Vietnam after Black Flags defeated White Flags rebellion. Pan Lunsi (also called Huang Chongying or Hoàng Sùng Anh), Wu Yazhong's lieutenant as well as his nephew, founded Yellow Flags and led them to Hà Dương, the former stronghold of White Flags. Their target was Vietnamese authorities. In March 1868, Wu Yazhong and Yellow Flags waged an attack on Cao Bằng. In October 1868, Yellow Flags conducted raids in Tuyên Quang. Phạm Chi Hương was appointed to organize a campaign against Wu Yazhong with the assistance of Ông Ích Khiêm. In July 1868, Vietnamese army was defeated in Lạng Sơn.

Qing dynasty appointed Feng Zicai to suppress Wu Yazhong in Vietnam. At the same time, Huế court also promoted Võ Trọng Bình to borderlands patrol commissioner who would serve as the link between Huế court and Feng Zicai. Feng Zicai's strategy which funded local militia to build up a “trap” for Wu Yazhong in the northern borderlands was cost-effective but successful. In late 1869, Wu Yazhong fled into Thái Nguyên and died there because of heavy wound. Another saying is that Wu Yazhong was killed by Ông Ích Khiêm when laid siege to Bắc Ninh in 1870. After Wu Yazhong's death, Yellow Flags and Black Flags were all that remained of Yanling Kingdom. Through levying taxation in opium trade and conducting pillages, these two bandits controlled the northern Vietnam. Later a brief battle happened between Yellow Flags and Black Flags, ending with Yellow Flags’ expulsion from Lào Cai.

Hoàng Kế Viêm and Imperial Bandits To solve the bandit problems in the borderlands, Đoàn Thọ was appointed as President of Tonkin military service. When Đoàn Thọ arrived at Lạng Sơn, a bandit named Tô Tứ sneaked on Lạng Sơn at midnight and killed Đoàn Thọ, while Võ Trọng Bình fled. In 1870, the Vietnamese court immediately assigned Hoàng Kế Viêm as governor of Lạng Sơn, Thái Bình and Ninh Bình to suppress banditry in northern Vietnam together with Tôn Thất Thuyết.

In the same year (1870), Pan Lunsi took control of his former base at Hà Dương in Tuyên Quang and later waged attack on Vietnamese soldiers. After defeating provincial government, the Yellow Flags occupied Tuyên Quang, threatening the Black Flags from east of Lào Cai. The action of Pan Lunsi stimulated heated debate in Huế court. Some mandarins were suspicious about the employment of Black Flags, but the court confirmed the official status of Liu Yongfu. When Pan Lunsi offered surrender twice to Huế court in 1871 and 1872, Hoàng Kế Viêm, the Black Flags’ supervisor, relayed the Vietnamese court's refuse of negotiation with Yellow Flags.

After killing Francis Garnier in 1873 when he attacked Hanoi, Liu Yongfu received a promotion to vice military commander in Vietnamese bureaucracy. Later in 1874, Hoàng Kế Viêm was appointed as governor-general of Sơn-Hưng-Tuyên and Tôn Thất Thuyết served as governor of Sơn Tây and consoler for military affairs. These two officials, together with Liu Yongfu, continued their struggle with Pan Lunsi and Yellow Flags which was the prior concern of Huế court. In early 1874, they sent out armies to chase after Yellow Flags from Tuyên Quang. In September, Pan Lunsi offered his surrender letter to Hoàng Kế Viêm, but the court still showed no interest and ordered governor of Hừng Hoa to demand Black Flags to capture Pan Lunsi. In March 1875, Huế court, refusing China's assistance in the war with Yellow Flags before, accepted Chinese help, appointed Tôn Thất Thuyết to cooperate with Qing military against Yellow Flags. Qing's army, led by Li Yangcai and Liu Yucheng, flushed most Yellow Flags out of Thái Nguyên, while Black Flags and Vietnamese military waited them in Hà Dương, the old base of Yellow Flags. The war ended with Pan Lunsi's mutilation and dismemberment. In 1878, Liu Yongfu began to receive monthly salary and supplies from Huế court as reward to his victory over the Yellow Flags. But not all the imperial officials accepted the Black Flags. Hoàng Kế Viêm, as the liaison between the court and Black Flags, encouraged Liu Yongfu and his men to cultivate land around Lào Cai so that he could not only mollify the critics towards Black Flags from imperial officials but also control the imperial bandit by binding them in one location. But his plan failed for Liu Yongfu had no interest in land cultivation or leaving Lào Cai. Later in March 1878, Black Flags conducted raids in Hưng Hóa province. At the same time, officials in Hanoi required a full report of Liu Yongfu’ actions through the court. Hoàng Kế Viêm promised to have a full investigation into Liu Yongfu, providing the account of his alleged crimes against Vietnamese authorities.

French Invasion French first attack on Tonkin (1873) In 1860s, Jean Dupuis, a French gun dealer, planned to sell his weapons to China where internal rebellions were happening. When Yunnan provincial military commander Ma Rulong was willing to purchase a shipment of rifles and ammunition, Dupuis decided to deliver these weapons by ships sailing from Hong Kong to Vietnam where he could use Red River to travel to Yunnan. With the help of Marcel Dupré, the governor-general of Cochinchina and the recognition of French Ministry of Navy, Dupuis prepared ships and crew, leaving Hong Kong in October 1872. During his journey, he had no fear of Vietnamese authorities for he had acquired a contract with Yunnan administration and a well-armed commercial vessel that every time he ignored the official orders to wait, directly leaving for next city. He exchanged foods and other materials with his weapons. He even met with Pan Lunsi and developed a close relationship with Yellow Flags. Hoàng Kế Viêm, in the middle of fighting against Yellow Flags with Feng Zicai and Black Flags, ordered officials to follow Dupuis, trying to detain him in Hưng Hóa. When Dupuis arrived in Yunnan, his contact with Yellow Flags was known by Huế court, and he was detained when he later returned to Hanoi.

Since French colonial controlled Cochinchina, French have been scouting Tonkin and Annam in order to find a path into China. In 1866, Francis Garnier joined Doudard de Lagree’s expedition to search for a way to China along Mekong River. Even though the road through Mekong failed, Garnier remarked that it was possible to use Red River as commercial link between Cochinchina and Yunnan.

As Jean Dupuis was detained, Francis Garnier returned to Cochinchina. Admiral Dupré assigned him the task to negotiate the release of Dupuis. Garnier presented a list of demands to the court, waiting for response, while Huế court did not take any action, suspicious of Garnier’s real intention. Because of that, Francis Garnier decided, without permission from his superior, to take over Hanoi by force on 20 November 1873. After capturing the city, Garnier and his men launched themselves in a full blown conquest of the region and conquered most of the delta's largest cities in the course of December 1873. There was divergence between Huế court and regional military officials. On the one hand, Huế court was preparing for negotiation with French representative to have Garnier remove from Hanoi. On the other hand, Hoàng Kế Viêm ordered Liu Yongfu and Black Flags to move to west of Hanoi, ready for offensive. On 21 December, a battle was started and ended with the decapitation of Francis Garnier by Black Flags’ platoon. The moment Huế court was informed of Garnier's death, court officials ordered Hoàng Kế Viêm and Tôn Thất Thuyết to move Liu Yongfu back to Hưng Hóa.

About Francis Garnier's death, there are different opinions. In 1973, Adrien Balny d’Avricourt wrote that the fatal attack on Garnier by Black Flags pirates was urged by the court which had never failed to apply the policy of duplicity. While the Vietnamese Veritable Records Dai Nam thuc luc recorded, Emperor Tự Đức’s intention was to solve the crisis with negotiation, and his order to Hoàng Kế Viêm and Tôn Thất Thuyết to prepare for military actions was reluctant and out of his distrust to French people. Hoàng Kế Viêm and Tôn Thất Thuyết were acting with deliberate calculation when they made sure that Liu Yongfu would cooperate and encouraged him to launch the attack, which exceeds the authority.