Skip to content

Vim, part 1: open a file, type, save

IntermediateLesson 28 of 667 min

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. x deletes a character, u undoes, G jumps 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.

KeyDoes
iinsert before the cursor
ainsert after the cursor
oopen a new line below and insert there
Escback to normal mode
arrows, or h j k lmove (in normal mode)
x, udelete 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.txt
G
o
Pay the gas bill<Esc>
:wq<CR>
cat todo.txt

Here <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.

Next