Learning how to use the vi text editor is essential for anyone working in a Linux environment. And while it can take some time to get used to how it works, mastering vi commands will help you edit files incredibly quickly without leaving your terminal or reaching for a mouse.

In this post, we cover the essential vi commands you need to navigate, edit, and save your text files like a pro.

What is vi editor?

Created by Bill Joy in 1976, the vi editor is a fast, lightweight text editor that lets you manipulate text files directly in a terminal window. Most modern Linux systems use an updated version called vim (vi improved), but the majority of vi commands work in both vim and vi. And since it's pre-installed on nearly every Unix-like system, knowing how to use vi is a useful skill for managing servers and writing scripts.

Understanding vi modes

To master vi commands, you must understand that vi is a modal editor. Unlike most programs where you just start typing, vi changes its behavior based on which mode you are in:

  • Command mode: This is the default mode when you open a file. Every key you press acts as a command—like moving the cursor or deleting a line—rather than typing text.
  • Insert mode: This is where you actually type text into the file. You enter this mode by pressing i and return to command mode by pressing the <esc> key.
  • Last line mode: When in command mode, typing a colon (:) drops you into a third distinct state where terminal level commands are executed.

To get started, use the following command in your terminal to open or create a file:

  • vi <filename> — Opens an existing file or creates a new one.

vi editor navigation

The vi editor always starts in command mode. While modern versions of vi let you use arrow keys, the traditional vi commands for movement let you keep your hands on the home row of your keyboard, so you can work faster.

  • h – Move cursor left
  • j – Move cursor down
  • k – Move cursor up
  • l – Move cursor right
  • 0 (zero) – Jump to the beginning of the current line
  • $ – Jump to the end of the current line
  • e – Jump to the end of the next word
  • w – Jump forward to the start of the next word
  • b – Jump backward to the start of the previous word
  • :1 – Go to the first line of the document
  • G – Go to the last line of the document
  • :[number] – Jump directly to the line number
  • :set nu – Display line numbers on the left hand side

Search and search/replace

Use these vi commands to quickly search for specific things in your file or do a global search and replace:

  • /[searchterm] – Search for [searchterm] moving forward in the file
  • n – Repeat the search forward
  • N – Repeat the search backwards
  • :%s/old/new/g – Global search and replace, replacing [old] with [new]

Entering and appending text

To add new text, you have to switch from command mode to insert mode. Use these vi commands to start inserting text exactly where you want it to go:

  • i – Insert text before the current cursor position
  • I – Insert text at the very beginning of the current line
  • a – Append text after the current cursor position
  • A – Append text at the very end of the current line
  • o – Open a new line below the current line and enter insert mode
  • O – Open a new line above the current line and enter insert mode

Editing, deleting, and managing text

Vi makes it easy to delete, copy, or move text with just a few keystrokes. These vi commands help you clean up your files quickly:

  • x – Delete the character under the cursor
  • dw – Delete from the cursor to the end of the word
  • dd – Delete (cut) the entire current line
  • [number]dd – Delete a specific number of lines
  • yy – Yank (copy) the current line
  • [number]yy – Yank (copy) a specific number of lines
  • p – Paste the deleted or yanked text after the cursor
  • P – Paste the deleted or yanked text before the cursor
  • u – Undo the last action

Saving your work and exiting

When you finish your work, press <esc> to make sure you are in command mode. Then, use last-line mode commands (which start with a colon) to save your file or exit the editor:

  • :w – Save (write) the file but stay in the editor
  • :wq – Save and quit
  • :q! – Quit and discard all unsaved changes
  • ZZ – A quick shortcut to save and quit (no colon needed)

Mastering the vi editor is a rite of passage for IT practitioners and Linux enthusiasts alike. While its modal nature takes some getting used to, committing these core vi commands to muscle memory will drastically accelerate your terminal workflow and server management efficiency. Keep this vi command reference bookmarked as you practice, and soon you'll be tearing through configuration files without your hands ever leaving the home row.

Learn more


FAQ: Common questions about the vi editor

1. Why should I use vi editor instead of a simpler editor like Nano?

Nano is beginner-friendly, but vi commands allow for much faster text manipulation once you learn them. Because vi is installed on almost every Unix-like system, it is essential for managing remote servers where other editors might not be available.

2. I'm stuck in vi editor and can't type! What do I do?

You are likely in command mode. Press i to enter insert mode so you can type. If you want to leave, press <esc> to return to command mode and type :q! to exit without saving.

3. What is the difference between vi and vim?

Vim stands for "vi improved." It includes features like syntax highlighting, multilevel undo, and better navigation. On most modern systems, such as Red Hat Enterprise Linux, typing vi actually opens vim in a compatibility mode.

4. How do I find a specific word in a large file?

In command mode, type / followed by the word you are looking for (for example, /searchterm) and press <enter>. Press n to jump to the next match or N to jump to the previous match.

5. How do I search and replace text globally?

To replace every instance of a word in a file, use the command :%s/old/new/g. The % tells vi to look at the whole file, and the g (global) means it replaces every instance on a line, not just the first one.

6. Can I recover a file if my connection drops while using vi?

Yes. vi usually creates a swap file (for example, .filename.swp). If your session ends abruptly, you can try to recover your work by running vi -r <filename> the next time you log in.


About the author

Deb Richardson joined Red Hat in 2021 and is a Principal Managing Editor, primarily working on the Red Hat Blog.

UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Virtualization icon

Virtualization

The future of enterprise virtualization for your workloads on-premise or across clouds