Linux vi Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful vi text editor on the Linux operating system. The lab covers the basic navigation and editing commands in vi, as well as more advanced operations. You will start by understanding the fundamentals of the vi editor, such as opening the editor, moving the cursor, entering insert mode, and saving or quitting the editor. Then, you will explore more advanced text editing techniques, including searching, replacing, and performing various operations on the text. By the end of this lab, you will have a solid understanding of how to effectively use the vi editor for efficient text processing and editing in the Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") subgraph Lab Skills linux/dd -.-> lab-422996{{"`Linux vi Command with Practical Examples`"}} linux/vim -.-> lab-422996{{"`Linux vi Command with Practical Examples`"}} end

Understand the Basics of vi Editor

In this step, you will learn the basic navigation and editing commands in the vi text editor. Vi is a powerful and widely-used text editor in the Linux environment, and understanding its fundamentals is crucial for efficient text processing.

First, let's start by opening the vi editor. In the terminal, run the following command:

vi

This will open the vi editor in the default mode, which is the command mode.

Now, let's explore some basic navigation and editing commands in vi:

  1. Moving the Cursor:

    • Use the arrow keys (up, down, left, right) to move the cursor.
    • Press h to move the cursor left, j to move down, k to move up, and l to move right.
  2. Entering Insert Mode:

    • Press i to enter the insert mode, which allows you to start typing and editing text.
    • While in insert mode, you can type, delete, and modify the text.
    • To exit the insert mode and return to the command mode, press the Esc key.
  3. Saving and Quitting:

    • To save the changes and quit the vi editor, type :wq and press Enter.
    • If you want to quit without saving, type :q! and press Enter.

Example output:

## Opening vi editor
$ vi

## Navigating in vi
## Press 'h', 'j', 'k', 'l' to move the cursor

## Entering insert mode
## Press 'i' to start typing

## Saving and quitting
## Type ':wq' and press Enter to save and quit
## Type ':q!' and press Enter to quit without saving

In this step, you will learn how to navigate and edit text within the vi editor.

First, let's open the vi editor by running the following command in the terminal:

vi

Now, let's explore some commands for navigating and editing text in vi:

  1. Moving the Cursor:

    • Use the arrow keys (up, down, left, right) to move the cursor.
    • Press h to move the cursor left, j to move down, k to move up, and l to move right.
    • Press w to move the cursor to the beginning of the next word, and b to move the cursor to the beginning of the previous word.
    • Press 0 to move the cursor to the beginning of the current line, and $ to move the cursor to the end of the current line.
  2. Editing Text:

    • Press i to enter the insert mode and start typing.
    • Press x to delete the character under the cursor.
    • Press dd to delete the entire line.
    • Press yy to copy the current line.
    • Press p to paste the copied text.

Example output:

## Open vi editor
$ vi

## Navigate using arrow keys, 'h', 'j', 'k', 'l'
## Move to next/previous word using 'w', 'b'
## Move to start/end of line using '0', '$'

## Enter insert mode and start typing
## Delete character using 'x'
## Delete line using 'dd'
## Copy line using 'yy'
## Paste using 'p'

Perform Advanced Operations in vi

In this final step, you will learn some advanced operations in the vi editor, which will help you become more efficient in text processing and editing.

First, let's open the vi editor by running the following command in the terminal:

vi

Now, let's explore some advanced commands in vi:

  1. Searching and Replacing:

    • Press / to enter the search mode, then type the word you want to search for and press Enter.
    • Press n to navigate to the next occurrence of the search term, and N to navigate to the previous occurrence.
    • To replace a word, type :%s/old_word/new_word/g and press Enter. This will replace all occurrences of "old_word" with "new_word" in the entire file.
  2. Working with Multiple Files:

    • Press :e filename.txt to open a new file in the vi editor.
    • Press :bn to switch to the next file, and :bp to switch to the previous file.
    • Press :ls to list all the open files in the vi editor.
  3. Executing Shell Commands:

    • Press :!command to execute a shell command without leaving the vi editor.
    • For example, :!ls -l will list the contents of the current directory.

Example output:

## Open vi editor
$ vi

## Search for a word using '/'
## Navigate to next/previous occurrence using 'n', 'N'
## Replace a word using ':%s/old_word/new_word/g'

## Open a new file using ':e filename.txt'
## Switch between files using ':bn', ':bp'
## List open files using ':ls'

## Execute shell commands using '!command'
## Example: ':!ls -l'

Summary

In this lab, you will learn the basic navigation and editing commands in the vi text editor, as well as how to perform advanced operations. First, you will understand the basics of the vi editor, including how to open it, move the cursor, enter insert mode, and save and quit. Then, you will learn how to navigate and edit text within the vi editor, such as moving the cursor, inserting and deleting text, and searching and replacing. Finally, you will explore advanced operations in vi, including working with multiple files, using macros, and customizing the editor.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like