How To Use The Unix Vi Editor: With Commands, Tips & Examples

Unix VI Editor

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

This simple yet powerful command-line editing tool remains a staple. You can utilize it to modify configuration files, compile a grocery list, draft a letter home requesting funds, develop a new script, or even alter source code.

This article explains everything you need to know about the Unix Vi Editor, how to launch it, a long list of shortcuts to learn and use, and what makes it better than the other text editors.

Let’s learn along!

What is a Unix Vi Editor?

The UNIX vi editor is a full-screen editor that operates in two modes: Command mode and Insert mode. In Command mode, every character you type acts as a command, whereas in Insert mode lets you add text to the file. For those new to vi, some essential commands are listed, indicated by an asterisk or star. Remember, both UNIX and vi are case-sensitive, so it’s best to use lowercase letters instead of uppercase for the best outcomes.

Why use vi instead of other editors?

Vi, which stands for “Visual Editor,” is a popular text editor found in Unix-like operating systems. It was created by Bill Joy in the late 1970s and is recognized for its strong text manipulation features.

Features

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

  • It boasts a simple design.
  • It is both lightweight and powerful.
  • Vi offers effective keyboard shortcuts.
  • Vi can be accessed on all Unix-like systems.
  • Vi operates as a modal text editor, meaning it has various modes for different functions.

How to Launch the vi Editor

The following table lists out the basic commands to use the vi editor:

Sr.No.Command & Description
1. vi filenameCreates a new file if it already does not exist, otherwise opens an existing file.
2. vi -R filenameOpens an existing file in the read-only mode.
3. view filenameOpens an existing file in the read-only mode.

Here is an example of how to create a new file named testfile if it does not already exist in the current working directory −

$vi testfile

The command above will produce the following output:

|<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>~<br>"testfile" [New File]

You will see a tilde (~) on each line after the cursor. A tilde indicates an unused line. If a line does not start with a tilde and looks blank, it may contain a space, tab, newline, or another invisible character.

You now have one file open to begin working on. The vi editor always kicks off in command mode. To start typing, you need to switch to insert mode by just hitting the i key. When you’re done typing and want to return to command mode, just press the Esc key.

Remember: If you’re ever confused about which mode you’re in, just tap the Esc key twice; that will definitely get you back to command mode. You can open a file with the vi editor commands in Unix, type a few characters, and then switch back to command mode to see how it all works.

Here’s a List of Unix Vi Editor Commands that are extremely useful when starting out:

  • Vi shortcuts
  • The best way to learn Vi is to create a new file and try it out for yourself. Feel free to use the common keyboard shortcut list below to help you learn Vi’s extensive vocabulary. This list of shortcuts is by no means exhaustive, but they will enable you to edit files and learn Vi in a short amount of time.
  • $ vi <filename> — Open or edit a file.
  • i — Switch to Insert mode.
  • Esc — Switch to Command mode.
  • :w — Save and continue editing.
  • yy — Yank (copy) a line of text.
  • :wq or ZZ — Save and quit/exit vi.
  • :q! — Quit vi and do not save changes.
  • o — Open a new line under the current line.
  • O — Open a new line above the current line.
  • A — Append to the end of the line.
  • a — Append after the cursor’s current position.
  • I — Insert text at the beginning of the current line.
  • b — Go to the beginning of the word.
  • e — Go to the end of the word.
  • dd — Delete an entire line.
  • x — Delete a single character.
  • Xdd — Delete X number of lines.
  • Xyy — Yank X number of lines.
  • G — Go to the last line in a file.
  • XG — Go to line X in a file.
  • gg — Go to the first line in a file.
  • k — Move up one line.
  • j — Move down one line.
  • h — Move left one character.
  • l — Move right one character.
  • :num — Display the current line’s line number.
  • p — Paste a line of yanked text below the current line.

vi vs Other Unix Text Editors

It’s Important to learn how the Unix Text Editor is different from other editors:

Feature / Editorvivim (Vi Improved)nanoemacsgedit
Interface TypeOnly Has a Command-line Command-line with enhancementsCommand-line, menu hintsCommand-line/GUI hybridGraphical (GUI) only
Learning CurveSteepModerate (easier than vi)EasyVery steepVery easy
Modes (Insert, Command)YesYesNoNoNo
CustomizationMinimalHigh (with plugins, themes)Very limitedExtremely high (Lisp-based)Limited
Common Use CaseQuick edits, default on serversDev environments, power usersBeginners, config file editingProgrammers, heavy customizationGUI users, quick editing
Syntax HighlightingNoYesPartialYesYes
Multiplatform SupportUnix/LinuxUnix/Linux, Windows, macOSUnix/Linux, macOSCross-platformMostly Linux/GNOME
Pre-installed on Unix?Yes (default)Often (or installable)OftenNo (manual install)No (desktop environments only)

Key Summary

Mastering the Unix vi editor is an essential and valuable skill for anyone who’s working as a system administration, programming or server management. The learning curve seems intimidating at first, but once you’ve started using it the power, the efficiency, and speed it offers makes it a developers favorite over time. Because it’s basic file editing and advanced command usage makes it reliable tool available accross virtually every Unix-based system. By understanding it’s modes, shortcuts, and necessary commands, ou can unlock a productivity powerhouse right form your terminal.

FAQ’s

1. What is the difference between Unix vi Editor from vim?
Vi Improved is what vim stands for. With additional features like syntax highlighting, improved navigation, and plugins, it’s a more comprehensive version of Unix Vi editor. Instead of using the original vi by default, many systems use vim.

2. Why am I unable to quit Vi Editor?
It is possible that you are in Command Mode. To exit without saving, press Esc, and then type :q!, or to save and exit, type :wq. Unbeknownst to them, new users frequently become stuck in Insert Mode.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

3. What are the various modes in the vi editor?
vi operates in three primary modes:

  • Command Mode: This is the default mode used for navigation and executing commands.
  • Insert Mode: This mode is for writing or editing text (press i to enter).
  • Visual Mode: This mode allows you to select and manipulate blocks of text.

4. How can I copy and paste in the vi editor?
To copy (yank) a line: use yy.

  • To paste below the current line: press p.
  • To paste above the current line: press P.

5. How can I undo a change in the vi editor?
To undo the last change: press u.

To redo a change: use Ctrl + r.

6. How can I search and replace text in vi?
To search, use:

/pattern (to search forward)
?pattern (to search backward)

To replace all instances in the file:

:%s/old/new/g

7. What’s the vi editor in Unix?
The vi Unix editor is a robust command-line text editor found on Unix and Unix-like systems. It’s famous for its efficiency, keyboard-driven editing, and the fact that it comes pre-installed on almost all Unix systems by default.

8. How can I open a file in the vi editor?
To open a file in vi, just use the command:

vi filename
Areeba Nauman
Areeba is a Content Writer with expertise in web content and social media, she can simplify complex concepts to engage diverse audiences. Fueled by creativity and driven by results, she brings a unique perspective and a keen attention to detail to every project she undertakes with her creativity and passion for delivering impactful content strategies for success. Let's connect on Linkedin: https://www.linkedin.com/in/areeba-bhatti/
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!