How to Use vi in Linux: Edit, Save, Quit and Find Text

To edit a file in vi, run vi filename, press i to type, then press Esc to return to command mode. Type :wq and press Enter to save and quit. Use :q! only when you want to leave without saving unsaved changes.

Updated : Corrected the movement keys, explained modes and save commands, and added a safe practice example. Vim-only features are identified separately.

Vi, Terminal Text Editor for Linux

Start with a practice file

Open a terminal and enter vi practice.txt in a folder where you can write files. Use a new name so you do not accidentally change a real document. If the command is missing, install a vi-compatible editor through your Linux distribution’s package manager.

  1. Press i and type two or three lines of text.
  2. Press Esc. The letters you press now act as commands.
  3. Type :w and press Enter to save.
  4. Type :q and press Enter to close the editor.

The ~ marks below the file are screen placeholders, not extra lines that will be saved. You can use vi for notes, scripts, and configuration files; it is not limited to programming.

Understand vi’s modes

Mode What it does How to reach it
Normal or command mode Move around and run editing commands Press Esc
Insert mode Type text into the file Press i, a, or o from normal mode
Colon command line Save, quit, or run commands such as :set number Press : from normal mode, then finish with Enter

Esc returns you to normal mode; it does not switch back and forth between both modes. If typing produces unexpected changes, press Esc before trying the next command.

Move through a file: h, j, k and l

Key Action in normal mode
h / l Move left / right
j / k Move down / up
w / b Move forward / backward by words
0 / $ Go to the start / end of the line
G Go to the last line
:25, then Enter Go to line 25

A number can repeat many commands: 3j moves down three lines. The Vim movement guide explains these shared basics in more detail.

Edit, copy and search

  • a adds text after the cursor; o opens a new line below.
  • x deletes the character under the cursor. dd removes the current line.
  • yy copies a line; p puts it after the cursor or below the current line for linewise text. 2yy copies two lines.
  • u undoes a change. Undo history differs between traditional vi and Vim.
  • /word, followed by Enter, searches forward. Press n for the next match.

Save and quit without a surprise

Command, then Enter Result
:w Save and keep editing
:wq Save and quit if saving succeeds
:q Quit; warns if unsaved changes would be lost
:q! Discard unsaved changes and quit

A permission error means the file could not be written. Do not assume adding ! grants administrator rights. Save a copy somewhere you control, then review the permissions.

On many Linux systems, vi opens Vim or another compatible editor. Vim adds features such as multiple undo levels and its own help system. See the official first-steps manual and, where installed, run vimtutor. If you are editing files on a home server, our XigmaNAS setup guide explains where the web interface is the better place to change settings.