Vim Basics (and How to Quit)

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

Modes

The surprising thing about vim is that it has modes. Keys do different things in different modes:

ModeHow to get thereWhat keys do
NormalVim starts here. Press Esc to come backKeys are commands: move, delete, copy, paste
InsertPress iKeys type text, like a normal editor. -- INSERT -- shows at the bottom
Command-linePress : in normal modeType a command such as :w and press Enter

Saving and quitting

From normal mode (press Esc first if you're not sure):

TypeDoes
:wSave (write)
:qQuit (only works if everything is saved)
:wqSave and quit
:q!Quit and throw away your changes

Editing in normal mode

KeysWhat it does
iInsert before the cursor
oOpen a new line below and start typing
xDelete the character under the cursor
ddDelete (cut) the whole line
yyCopy (yank) the line
pPaste below
uUndo
/wordSearch for word; n for the next match
gg / GGo to the first / last line

Your first vim edit

  • vim todo.txt opens the file in normal mode.
  • Press i and type some text.
  • Press Esc to go back to normal mode.
  • Type :wq and 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, :wq to save and quit, :q! to quit without saving.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…