If you log into a completely fresh, minimal installation of a Linux server, you might find that user-friendly text editors like nano are not installed. However, you are absolutely guaranteed to find the vi text editor (or its modern successor, vim). Learning the absolute basics of vi is a mandatory survival skill for any Linux administrator. Because vi is a modal editor—meaning the keys on your keyboard perform entirely different actions depending on what “mode” the editor is currently in—it is notoriously difficult for beginners to figure out how to simply type a word and save the file.
Opening a File
To open a file in vi, you simply type the command followed by the filepath.
- Open your Linux terminal.
- Type the command:
vi /etc/hosts - Press Enter.
The file will open. You can use your keyboard’s arrow keys to move the blinking cursor around the screen. Do not start typing letters yet.
Understanding Modes: Command vs. Insert
When vi opens, it starts in Command Mode. In this mode, pressing keys on your keyboard executes commands (like deleting a line or searching for text). If you press the “x” key, it will not type an “x”—it will delete the character under your cursor.
To actually type text into the document, you must switch to Insert Mode.
- Move your cursor to the exact location where you want to add text.
- Press the `i` key (for Insert) on your keyboard once.
- Look at the absolute bottom-left corner of your terminal screen. You should now see the word — INSERT —.
- You can now type normally. Every key you press will be inserted into the document as text.
When you are finished typing your changes, you must immediately exit Insert mode. Press the `Esc` (Escape) key on your keyboard. The “– INSERT –” text at the bottom will disappear, confirming you are safely back in Command Mode.
How to Save and Quit (The Most Important Lesson)
Thousands of developers have been trapped inside vi because the quit command is incredibly unintuitive. To save and exit, you must be in Command Mode (press Esc to be sure) and then use the “colon” command prompt.
- Ensure you are in Command Mode by pressing Esc.
- Type a colon:
:(Hold Shift and press the semicolon key). Your cursor will instantly jump to the bottom-left corner of the screen next to the colon. - Type the letter
w(for Write, which means save). - Type the letter
q(for Quit). - Your command prompt at the bottom should now read
:wq. - Press Enter.
The editor will save your changes and instantly drop you back into your normal Linux terminal.
Emergency Quit: If you made a horrible mistake, accidentally deleted half the file, and want to exit without saving any changes, press Esc, type :q! (the exclamation point forces it to quit without saving), and press Enter.