how to exit vi in linux

C++
Vim editor:
Insert: hit I key
Discard and exit: esc q!
Save and exit: esc qw(without saving changes)
if in insert or append mode press Esc.
else type ":q!" and press Enter
Source

Also in C++: