vi /etc/hosts or sudo vi/etc/hosts
you have to write :w! to write or :wq!
Press Esc to enter Command mode, and then type
:wq to write and quit the file. The other, quicker option is to use the keyboard shortcut
ZZ to write and quit. To the non-vi initiated, write means save, and quit means exit vi.
source An introduction to the vi editor.
with ! you modify the file