Skip to content

Edit files with vi ​

Ubuntu Terminal includes vi, a small editor that runs inside the terminal. This page gets you through opening a file, changing it, saving and leaving.

Typing vim starts the same editor. It is the compact BusyBox vi, with Vim-style keys. It is not full Vim or Neovim, so plugins, syntax colours and .vimrc settings are not available.

The idea: two modes ​

vi is always in one of two modes.

  • Command mode: keys are commands. x deletes a character, dd deletes a line. This is the mode vi starts in.
  • Insert mode: keys type text, as in any editor.

Press i to go from command mode to insert mode. Press Esc to go back. If you are ever unsure which mode you are in, press Esc.

Edit a file ​

  1. Open a file. If it does not exist, vi starts with an empty one and creates it when you save.

    bash
    vi notes.txt
  2. Move to where you want to type, with the arrow keys.

  3. Press i. You are now in insert mode. Type your text.

  4. Press Esc to return to command mode.

  5. Type :w and press Enter to save.

  6. Type :q and press Enter to quit.

You can do the last two steps in one: :wq saves and quits.

Keys you need ​

Use these in command mode. Press Esc first if you were typing.

KeysWhat they do
iInsert text before the cursor
aInsert text after the cursor
oOpen a new line below and start typing
xDelete the character under the cursor
ddDelete the current line
yyCopy the current line
pPaste the copied or deleted line below
uUndo the last change
/word then EnterSearch for word. Press n for the next match
ggGo to the first line
GGo to the last line
:wSave
:qQuit
:wqSave and quit
:q!Quit and throw away your changes

Leave without saving ​

If you changed something by accident, press Esc, type :q! and press Enter. The file on disk stays as it was.

If :q refuses to quit, it is because you have unsaved changes. Use :wq to keep them or :q! to drop them.

Where vi appears on its own ​

Other programs open vi when they need you to write something. The most common case is git commit without -m: Git opens vi for the commit message. Type the message, then Esc, :wq and Enter.

To avoid the editor, give the message on the command line:

bash
git commit -m "Describe the change"

Prefer another editor? ​

Any editor installed on Windows that can be started from the command line also works here. If Visual Studio Code is installed, this opens a file in it:

bash
code notes.txt

Next steps ​