Why learn vim?
vim (or its older version vi) is installed on almost every Linux machine in the world, even tiny servers that have no nano. Sooner or later you will land in it, so you need the basics, starting with how to get out.
vim todo.txtModes
The surprising thing about vim is that it has modes. Keys do different things in different modes:
| Mode | How to get there | What keys do |
|---|---|---|
| Normal | Vim starts here. Press Esc to come back | Keys are commands: move, delete, copy, paste |
| Insert | Press i | Keys type text, like a normal editor. -- INSERT -- shows at the bottom |
| Command-line | Press : in normal mode | Type a command such as :w and press Enter |
Saving and quitting
From normal mode (press Esc first if you're not sure):
| Type | Does |
|---|---|
:w | Save (write) |
:q | Quit (only works if everything is saved) |
:wq | Save and quit |
:q! | Quit and throw away your changes |
Editing in normal mode
| Keys | What it does |
|---|---|
| i | Insert before the cursor |
| o | Open a new line below and start typing |
| x | Delete the character under the cursor |
| dd | Delete (cut) the whole line |
| yy | Copy (yank) the line |
| p | Paste below |
| u | Undo |
| /word | Search for word; n for the next match |
| gg / G | Go to the first / last line |
Your first vim edit
vim todo.txtopens the file in normal mode.- Press i and type some text.
- Press Esc to go back to normal mode.
- Type
:wqand press Enter to save and quit.
Think of it like this: Think of normal mode as holding a remote control, and insert mode as holding a pen. With the remote you can jump around and delete things, with the pen you write. i picks up the pen, Esc puts it down.
Tip: Lost in vim? Press Esc a couple of times, type
:q!and press Enter. You're out, and nothing was changed.
Remember: i to type, Esc to stop typing,
:wqto save and quit,:q!to quit without saving.