Yes, Imam Ali (AS) was always accompanying the Prophet Muhammad (SAWA) including when going to Hiraa' cave and he used to pray and worship Allah when the Prophet was worshiping Allah. Even during the revelation Ali (AS) was with the Prophet but he was not a Prophet. The Prophet told Ali: Surely, you hear what I hear and see what I say but you are not a Prophet, you are my successor إنك تسمع ما أسمع وترى ٬ما أرى (لكنك لست بنبي وإنما أنت
وزير
Nahjul Balaghah, sermon 192.
Wassalam.