Help Docs Server Administration Linux Server Administration File and Directory Management in Linux Introduction to the vi Text Editor

Introduction to the vi Text Editor

vi/vim is a modal Unix text editor. Master Insert, Normal, and Overwrite modes to navigate, edit, save, and search files efficiently.

The vi editor (and its improved version, vim, which stands for Vi IMproved) is a visual text editor widely used on Unix and Linux systems. While it can seem confusing to new users, it is incredibly powerful once you understand its core concepts.

One of the most important concepts to grasp is that vi is a modal editor. This means the editor behaves differently depending on which mode you are currently in.

  • Normal Mode (also called Command Mode): This is the default mode when you open vi. In this mode, you cannot type text directly. Instead, this mode is used for moving the cursor, deleting text, and issuing commands to the editor (like saving or quitting).
  • Insert Mode: In this mode, you can type text normally into the file.
  • Overwrite Mode: In this mode, anything you type replaces the existing text at the cursor position.

You need to enter Insert or Overwrite mode to actually type anything.

Getting Started with vi/vim

If you have done a full vim installation on your server or workstation, an excellent way to learn the editor is by running the vimtutor command.

Alternatively, you can start using vi directly.

Invoke vi by typing the command followed by the name of the file you want to edit (or a new file name).

vi test.txt

vi will start up and display the file (or an empty buffer if it’s a new file). You will start in Normal mode.

Entering and Exiting Modes

To start typing text, you need to switch from Normal mode to Insert mode.

Type i to enter Insert mode. You can now enter text normally.

i

When you are finished typing and want to perform other actions (like moving the cursor efficiently or saving), you must return to Normal mode.

Hit esc (the Escape key) to return to Normal (command) mode.

esc

To enter Overwrite mode at the cursor position, you use the R command from Normal mode.

R

Once in Overwrite mode, typing characters will replace existing ones. To exit Overwrite mode and return to Normal mode, hit esc.

Moving Around the File

While in Normal mode, you can move the cursor. You can often use your keyboard’s cursor (arrow) keys. If the terminal settings aren’t properly configured, you can use the jkl; row keys.

Move the cursor down one line.

j

Move the cursor up one line.

k

Move the cursor left one character.

h

Move the cursor right one character.

l

You can also prefix these commands with a number to repeat the action multiple times. For example, to move down four lines:

4j

There are many other movement commands available in Normal mode:

Go to the first line of the file.

gg

Go to the last line of the file.

G

Go to a specific line number (e.g., line 4).

:4

Alternatively, you can use the G command with a line number prefix (e.g., line 4).

4G

Go to the end of the current line.

$

Go to the beginning of the current line (non-blank character).

^

Go to the beginning of the current line (column 0).

0 (zero)

Go to the end of the current word.

e

Go to the end of the current Word (Words are separated by spaces, not just punctuation).

E

Go to the beginning of the current word.

b

Go to the beginning of the current Word.

B

Move forward one screen.

ctrl-f

Move backward one screen.

ctrl-b

Text Manipulation in Normal Mode

From Normal mode, you can issue commands to manipulate text.

Delete the character under the cursor.

x

Start insert mode at the cursor location.

i

Start insert mode at the beginning of the current line.

I

Start insert mode at the end of the current line.

A

Create a new line after the current line, move the cursor there, and enter insert mode.

o

Create a new line before the current line, move the cursor there, and enter insert mode.

O

Start overwrite mode at the cursor location.

R

Replace just one character at the cursor location (you then return to Normal mode).

r

Delete everything from the cursor to the end of the line, then start insert mode.

C

Delete everything from the cursor to the end of the current word, then start insert mode.

cw

Delete the entire line the cursor is on.

dd

You can combine d with movement commands to delete multiple lines or words. For example:

Delete the line the cursor is on, and the next four lines down.

d5j

Delete the line under the cursor and the one immediately above.

d2k

Delete the line under the cursor and all the lines below it.

dG

Delete the current word.

dw

Copy (yank) the current line.

Y

Copy the current word and the next word.

y2w

Paste a copied (yanked) line after the cursor position.

p

You can paste multiple copies by prefixing with a number. Paste three copies of the last copied text after the cursor position.

3p

Repeat the last command.

.

Repeat the last command four times.

4.

Undo the last command.

u

Undo the last eight commands.

8u

Saving and Quitting

To save your changes and/or quit the editor, you issue commands from Normal mode using the colon (:) prompt. Type : and then the command, followed by pressing Enter.

Quit vi. If you have unsaved changes, vi will warn you.

:q

Write out (save) the current buffer to the current file, then quit vi.

:wq

Write out the current buffer to the current file. This is not necessary if you already have the file open but is useful if you want to save changes periodically.

:w

Write the current buffer out to a specified filename. This is necessary if you want to save a copy of the file to a different location or under a new name.

:w filename

Force write. If you are the owner of a file marked as read-only, this allows you to save changes anyway.

:w!

Quit vi immediately, discarding any changes made since the last save. This is useful if you’ve made changes to a configuration file, for example, and aren’t sure if you’ve messed it up.

:q!

Searching

From Normal mode, you can search for patterns within the file.

Find the next occurrence of pattern in the buffer.

/pattern

Be careful when searching; the search pattern is interpreted as a regular expression. Characters like $, ., /, and ^ will be evaluated as operators. You might not get the results you expected unless you are familiar with regular expressions.

After performing a search with /, you can find the next occurrence of the pattern.

n

Find the previous occurrence of the pattern.

N

Vim Specific Features

If you are using vim, there are additional useful commands, particularly for making the text easier to read.

Enable syntax highlighting. vim uses the file extension to determine what type of highlighting to apply.

:syntax on

Change the background setting for syntax highlighting to show up better on a terminal with a dark background.

:set bg=dark

Change the background setting for syntax highlighting to show up better on a terminal with a light background.

:set bg=light

vim also has powerful search and replace capabilities using regular expressions.

Replace all instances of find-string with replace-string globally in the file.

:%s/find-string/replace-string/g

Again, unless you are comfortable with regular expressions, it’s recommended not to use this command for anything more complex than simple alphanumeric strings, as even characters like . are regular expression operators.

Mastering vi or vim takes practice due to its modal nature, but understanding these basic commands for movement, text manipulation, saving, and quitting will allow you to effectively edit files on your server.

Was this article helpful?