How to Use the vi Text Editor in Ubuntu: A Beginner’s Guide

Imagine you are managing a remote Ubuntu server via SSH and need to update a critical configuration file, but there is no graphical interface available. You open the file with a tool you’ve heard of, but suddenly your arrow keys don’t work, you can’t type anything, and you can’t even figure out how to close the window. This “locked-in” feeling is a rite of passage for almost every Linux user who encounters the vi editor for the first time.

Learning to use vi (or its improved version, Vim) is essential because it is the most widely available text editor across all Linux distributions. Whether you need to edit a log file, or you are configuring a complex network script, vi is the “universal” tool that will always be there to help you get the job done.

Key Takeaways: Mastering vi Basics

  • Modal Editing → Unlike standard editors, vi uses different modes for navigating, typing, and executing system commands.
  • Insert Mode → You must press the i key before you can start typing or modifying text.
  • Command Mode → This is the default starting state used for navigation and text manipulation like deleting or copying.
  • Saving and Quitting → All file operations like saving (:w) or exiting (:q) are done through Extended Command Mode.
  • Vim vs. vi → Ubuntu typically installs Vim (Vi IMproved), which adds features like syntax highlighting and an internal tutorial.

Method 1: The Essential Editing Workflow

For most tasks, you only need a handful of commands to open, edit, and save a file. This is the “minimum” workflow required for any administrative task.

1. Opening a File To open a file for editing, type the command followed by the filename. vi my_config.txt

2. Entering Text Once the file is open, you are in Command Mode. To start typing, press i. You will see -- INSERT -- appear at the bottom of the terminal.

3. Deleting and Undoing If you make a mistake, press Esc to return to Command Mode, then:

  • Press x to delete the character under the cursor.
  • Press u to undo your last action.

4. Saving Your Work To save and exit, ensure you are in Command Mode (press Esc), then type :wq and press Enter.

Method 2: Advanced Selection with Visual Mode

If you need to move large blocks of text or delete entire paragraphs, Visual Mode is the most efficient method.

Command Examples:

  • v → Starts Character-based selection.
  • Shift+V → Starts Line-based selection for entire rows.
  • Ctrl+V → Starts Block-based selection, perfect for data columns.

Execution Steps:

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

  1. Position your cursor at the start of the text.
  2. Press v to enter Visual Mode.
  3. Use the arrow keys (or h, j, k, l) to highlight the text.
  4. Press y to yank (copy) the text or d to delete it.
  5. Move to the new location and press p to put (paste) the text.

Step-by-Step Process: Creating a Bash Script in vi

Follow these steps to create and save a basic script in Ubuntu:

  1. Launch the editor: Type vi hello_world.sh in your terminal.
  2. Enter Insert Mode: Press the i key.
  3. Type the script content: #!/bin/bash echo "Hello from Ubuntu!"
  4. Exit Insert Mode: Press the Esc key.
  5. Enter Extended Command Mode: Type a colon (:).
  6. Save and Quit: Type wq and press Enter.
  7. Verify: Use the command cat hello_world.sh to see your saved text.

Summary Tables

Vim ModeKey to EnterPrimary Purpose
Command ModeEscNavigation, deleting, and copying.
Insert ModeiTyping and adding new text.
Extended Command:Saving, quitting, and searching.
Visual ModevHighlighting and manipulating blocks.
Essential CommandAction Performed
:wWrite (save) the file changes.
:q!Quit without saving changes (discard).
:wqWrite and Quit simultaneously.
uUndo the last edit.
xDelete a single character.

FAQ

What if I get stuck and can’t type or exit? Always press the Esc key a few times to ensure you are in Command Mode. Once there, you can type :q! to force the editor to close without saving anything.

Why do some people use h, j, k, and l to move? In early versions of vi, keyboards often lacked dedicated arrow keys. These four letters are grouped together and allow you to navigate without moving your hands from the home row: h (left), j (down), k (up), and l (right).

How can I learn vi more quickly? Ubuntu comes with a built-in interactive tutorial. Simply type vimtutor in your terminal to start a 30-minute hands-on lesson.


Related Posts