Vim, part 1: open a file, type, save
Open a file in Vim, switch between normal and insert mode with i and Esc, add a line and save with :wq. Practise in a real-feeling Vim in your browser.
Sooner or later a terminal hands you Vim. It's on nearly every Linux machine and server, often as vi, and programs such as git open it when they want you to write something. A few keys are enough to get your work done.
Modes
The one idea to get: Vim has modes.
- In normal mode, where it starts, keys are commands.
xdeletes a character,uundoes,Gjumps to the last line. Typing words here does strange things. - In insert mode, keys type text, as in any editor. The bottom line says
-- INSERT --.
You go into insert mode with a key, and back to normal mode with Esc.
| Key | Does |
|---|---|
i | insert before the cursor |
a | insert after the cursor |
o | open a new line below and insert there |
| Esc | back to normal mode |
arrows, or h j k l | move (in normal mode) |
x, u | delete a character, undo |
Saving
In normal mode, : opens the command line at the bottom. :w then Enter writes (saves) the file; :wq saves and quits.
vim todo.txtGoPay the gas bill<Esc>:wq<CR>cat todo.txtHere <Esc> means the Esc key and <CR> the Enter key, the way Vim writes them.
Tip:Lost? Press Esc once or twice. You're back in normal mode, and the bottom line shows what Vim is doing.
Your turn
Open todo.txt in Vim, add a new last line that says Water the plants, then save and quit.
Your task
Open todo.txt in Vim, add a new last line that says Water the plants, then save and quit.
- Open todo.txt in Vim (not done yet)
- Add "Water the plants" as the last line, saved by Vim (not done yet)
- Quit Vim (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
vim- Edit text files in the terminal (a practice subset of Vim) More on vim
i- Vim: insert mode, to type text before the cursor
o- Vim: open a new line below and type on it
Esc- Vim: back to normal mode, where keys are commands
:w- Vim: write (save) the file
:wq- Vim: save and quit
Every everyday command on one page: the Linux commands cheat sheet.