How to save + close file when editing in bash?
Esc will only throw you back into command mode in VI or Vim. To Save and quit press Shift + Z + Z, :wq, or :x in command mode. If you are opening the file in read only mode you will have to hit :q!.
Consider looking at this cheatsheet as well for more macros VI cheatsheet