With eyes fixed on a Peruvian campaign it was only natural that San Martin should leave immediate details in Chile to others. Though all central Chile submitted with good grace, the South remained a stronghold of the Spanish sympathisers. Among its warlike people the royalist armies had been recruited, and there lay the two strongest fortresses—Talcahuano and Valdivia—both of them still in possession of the Spaniards. After two months' delay, Las Heras, with a thousand men, was despatched, but his force was inadequate and his advance slow. Before he arrived near Concepcion, an able Spanish general, Ordoñez, who had fought side by side with San Martin in Spain, had organised a division equal in numbers, with which he retired to Concepcion and there was joined by the sixteen hundred troops who had escaped after the rout at Chacabuco, and who had been ordered back to Chile the moment they made their appearance at Callao. The Spanish general now thought himself strong enough to annihilate Las Heras, but the sortie which he led was beaten back in the battle of Gavilan. However, this victory was in no way decisive, and the patriots were not able to make any impression on the fortifications at Talcahuano or to advance south of the Biobio. Southern Chile remained hostile and Talcahuano and Valdivia were open doors through which the Spaniards could send reinforcements and supplies as long as they held command of the sea.
San Martin remained in Santiago only a short time after Chacabuco. Prepossessed with the idea that Chile could never be safe or Peru won until he had organised a fleet to wrest control of the Pacific from the Spaniards, he hastened across the Andes to arrange with his friends in the Argentine government for the necessary money. The Chilean campaign had saved Buenos Aires from impending invasion; the Argentine patriots would certainly be crushed if Chile should fall back into Spanish hands; they could never feel secure so long as Peru and Bolivia remained royalist. The promises which he asked were readily given, on his agreeing that Chile should contribute three hundred thousand dollars toward the purchase of a squadron on the Pacific, and forty thousand for the support of the Argentine army on the Bolivian frontier, besides taking the responsibility of the pay and maintenance of the Army of the Andes. Argentina was to aid in purchasing the fleet and hold back the Spaniards on the Bolivian border.
San Martin returned to Chile, where he was shortly followed by an official representative of the Argentine government and the alliance created by Chacabuco received formal sanction. He found Chilean affairs in a very unsatisfactory condition. O'Higgins was hated by the powerful partisans of the Carreras, and distrusted by Chileans generally as too much under Argentine influence. His power really rested upon Argentine bayonets; his appointment of Quintana, an Argentine and San Martin's aide-de-camp, as acting dictator at Santiago was bitterly resented. San Martin's presence did something to allay the feeling, but as a matter of fact he had little sympathy for the Chilean people, being a man who despised the arts by which popularity is gained, and who made few friends. Meanwhile the three Carreras were actively plotting from their exile at Buenos Aires for the overthrow of O'Higgins and San Martin. Their friends and agents swarmed in Chile and preparations were made for a rising as soon as they should set foot in the country. The two younger brothers attempted to cross the Andes in disguise, but were detected and arrested at Mendoza. Quintana ordered the imprisonment of many persons suspected of being Carrera partisans, but his severe measures raised national feeling to such a height that it was thought safest to carry out San Martin's suggestion and appoint a Chilean as acting dictator in his stead.
In the Argentine the position of the patriot government was even worse. With civil war actively raging in the one country and only held in check by foreign bayonets in the other, and with both governments struggling against financial difficulties, it is no wonder that the war-ships which were expected to sweep the Spanish frigates from the Pacific did not arrive. The delay cost the patriots dear. In January, 1818, four Spanish ships, mounting two hundred and thirty cannon sailed into Talcahuano, and landed three thousand four hundred well-equipped soldiers, most of them peninsular veterans. San Martin, a master of the art of recruiting, had raised a second army composed principally of Chileans and nearly equal in numbers to the original Army of the Andes, so that his total force amounted to nine thousand men, while the Spanish troops did not exceed five thousand. The Argentine general was in the dark as to where the enemy would land, and had already issued orders for O'Higgins, who was in command near Concepcion, to retreat, resolved on concentrating his forces near Valparaiso. Even after the Spanish army had disembarked at Talcahuano, San Martin was in doubt whether Osorio would not re-embark and strike at some unexpected harbour near Santiago. But the latter came up steadily by the land route, encountering no opposition though somewhat hampered by broken bridges and the bareness of the country of horses and supplies, for the retreating O'Higgins had left his track a desert. The farther the Spaniards penetrated toward Santiago the more difficult became the feeding of their army and the more certainly disastrous a retreat in case of reverse.
O'Higgins stopped at Talca to await orders, and there, on the 20th of January, 1818, he defiantly made proclamation of Chile's absolute independence of Spain. Three weeks later the approach of Osorio's army forced him to abandon the place and he retired to form a junction with San Martin. The latter completed his concentration and advanced with an army of over seven thousand men, superior in all arms and especially in cavalry and artillery. About a hundred miles south of Santiago he met the Spaniards and won some cavalry skirmishes. The enemy retired toward Talca, unwilling with inferior forces to bring on a general action where defeat meant annihilation, and even contemplating a retreat to Talcahuano. But behind them lay the deep river Maule, and San Martin made a dash to reach it first. The two armies marched rapidly on parallel lines with the patriot cavalry harassing the Spanish rear. On the afternoon of the 19th of March the Spaniards wheeled into line in excellent position just outside the city of Talca, with their west flank protected by a stretch of broken ground called the Cancha-Rayada. San Martin was following close, but the partial attack which be immediately made was interrupted by darkness before any decisive results were obtained. Hastily going into camp too near the enemy's lines and all unprepared for battle, the patriots were surprised at about nine o'clock in the evening by the assault of the whole Spanish army. The alarm was given by the cavalry pickets, but only a few had time to get into line of battle before the enemy was upon them. San Martin over on the extreme right heard a few volleys and then the noise of confused flight, scattering shots, and the thundering hoof-beats of the pursuing cavalry. O'Higgins had been wounded while trying to get his men into order, and from that moment the patriots in his neighbourhood thought of nothing but escape through the darkness. The centre and left, including the cavalry, dispersed in the wildest confusion, abandoning the artillery. The right wing, composed of three thousand five hundred infantry, was not attacked and waited in stupefaction for two or three hours not clearly understanding what had happened. Its officers held a council, put Las Heras in command, and by daybreak the division was sixteen miles from the field of battle. In the meantime San Martin and O'Higgins had found each other, and soon were busily engaged in collecting the scattered cavalry. The patriot loss in killed and wounded had been small, but a third of their number had deserted and many of the remainder searched in vain for their regiments. However, the royalist army had been nearly as badly dispersed in making this night attack as the patriots in receiving it. No effective pursuit could be made, and San Martin retreated on Santiago practically unmolested. The first news of the disaster was carried to the capital by fugitive officers. They reported that San Martin was killed and O'Higgins mortally wounded, and everything lost. Shouts of "Viva el rey" resounded through the streets; leading citizens opened communication with Osorio, and the republicans prepared for flight to Mendoza or Valparaiso. But the next day word came that San Martin himself was safe; and the day following a despatch saying he had four thousand men under his orders. With O'Higgins's arrival in the city the revolutionary disorders were suppressed, and soon San Martin rode into the city. Though half dead through loss of sleep, as he drew rein at his house he made the one speech of his life, laconically assuring the people that he expected to win the next battle, and that right soon.
Not forgetting precautions which ensured a safe retreat to the northern provinces or the Argentine, he devoted himself to re-organising the army, and within ten days after its dispersal had five thousand men together, well provided and resolute to give a good account of themselves. He took a position on a low line of chalk hills seven miles south-west of Santiago, and waited for the enemy, whose numbers were now slightly superior to his own. Meanwhile the Spanish officers were greatly disappointed at the negative results of Cancha-Rayada; mutual reproaches flew back and forth in their council of war; many advocated maintaining the defensive and even retreating to the south to be nearer their base. Their indecision gave San Martin the needed opportunity to gather his dispersed forces and to inspire them with his own confidence. Finally, however, Osorio advanced cautiously on Santiago, hoping that the Argentine would not risk another battle for the defence of the capital, and manœuvring to the west so as to get between the city and the sea. In front of San Martin's position lay another line of chalk hills, separated from the first by a narrow stretch of low ground. At their western end ran the road from Santiago to Valparaiso. Like the Union position at Gettysburg this line of hills was admirably adapted for a defensive battle, and Osorio resolved to occupy it, especially as he thought his left wing extended far enough west to command the Valparaiso road, thereby securing him a communication with a new and more convenient base on the coast and giving him a line of retreat in case of a reverse. But San Martin's quick eye saw that this opinion was mistaken; and that his opponent might easily be cut off.