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.
xdeletes a character,dddeletes a line. This is the modevistarts 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
Open a file. If it does not exist,
vistarts with an empty one and creates it when you save.bashvi notes.txtMove to where you want to type, with the arrow keys.
Press i. You are now in insert mode. Type your text.
Press Esc to return to command mode.
Type
:wand press Enter to save.Type
:qand 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.
| Keys | What they do |
|---|---|
i | Insert text before the cursor |
a | Insert text after the cursor |
o | Open a new line below and start typing |
x | Delete the character under the cursor |
dd | Delete the current line |
yy | Copy the current line |
p | Paste the copied or deleted line below |
u | Undo the last change |
/word then Enter | Search for word. Press n for the next match |
gg | Go to the first line |
G | Go to the last line |
:w | Save |
:q | Quit |
:wq | Save 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:
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:
code notes.txt