What is Vi Editor? Modes of Vi Editor

What is Vi Editor? Modes of Vi Editor

  • Vi is a powerful text editor that is commonly available on Unix-like operating systems.
  • It's a modal editor, meaning it has three primary modes: Normal mode, Insert mode, and Command-line mode.
  • In this guide, we'll cover the basics of Vi, the three modes, and how to write, save, and execute a shell script using Vi Editor.

Three Modes of Vi Editor

1. Normal Mode

  • In Normal mode, Vi is used for navigation and manipulation of text. You can't directly type text in this mode.
Here are some common commands:
  • h: Move left
  • j: Move down
  • k: Move up
  • l: Move right

Editing:

  • i: Enter Insert mode before the cursor position
  • x: Delete the character under the cursor
  • dd: Delete the current line
  • yy: Copy (yank) the current line
  • p: Paste the previously yanked text

Insert Mode

  • In Insert mode, you can type text into the editor.
  • To enter Insert mode, press i in Normal mode.
  • To exit Insert mode and return to Normal mode, press Esc.

  • In Command-line mode, you can execute commands such as saving or quitting the editor.
  • To enter Command-line mode, press : in Normal mode.
  • Here are some common commands
:w: Save the file
:q: Quit the editor
:wq: Save and quit

Writing a Shell Script in Vi Editor

  • Now, let's write a simple shell script using Vi Editor. A shell script is a text file that contains a sequence of commands to be executed by a shell (like Bash).
  • Open Vi Editor by typing vi followed by the script filename. For example, vi myscript.sh.
  • You will start in Normal mode. Press i to enter Insert mode, so you can type your script.
  • Write your shell script. Here's an example of a simple script that prints "Hello, World!" to the terminal:
1#!/bin/bash
2echo "Hello, World!"
3
4
  • After writing the script, press Esc to exit Insert mode and return to Normal mode.
  • To save your script, enter Command-line mode by pressing :. Then, type w and press Enter. This will save the file.
  • To exit Vi Editor, again enter Command-line mode by pressing : and type q. Press Enter to quit.
Now you have successfully written and saved a shell script using Vi Editor

Executing the Shell Script

To execute the shell script you've just created, follow these steps:
In your terminal, navigate to the directory where your script is located.
Make the script executable using the chmod command:
1chmod +x myscript.sh
2Run the script by typing:
3./myscript.sh
4
You should see the output "Hello, World!" on your terminal.
Vi Editor can be challenging for beginners, but with practice, it becomes a powerful and efficient tool for editing text and writing scripts on Unix-like systems.
This guide should help you get started with the basics.

Getting Started with Vi Editor on Windows and macOS

Vi is a text editor commonly used in Unix-like environments. However, it can also be used on Windows and macOS.
Here's a step-by-step guide to getting started with Vi Editor on both platforms.

On Windows

Using GVim (Graphical Vi)

1-Download GVim:
2-Launch GVim:
  • Search for "GVim" in your Windows search bar and open it.
3-Working in GVim:
  • GVim has the same modes (Normal, Insert, Command-line) and functionality as Vi on Unix-like systems.
  • To enter Insert mode, press i. To exit Insert mode, press Esc.
  • To enter Command-line mode, press :

Using Windows Subsystem for Linux (WSL)

If you're using Windows 10 or later, you can use Vi within the Windows Subsystem for Linux (WSL), which provides a Linux environment. Follow these steps

1-Enable WSL:

  • Open PowerShell as an administrator.
  • Run the following command to enable WSL:
1dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestar
Install a Linux Distribution:
  • Install a Linux distribution from the Microsoft Store (e.g., Ubuntu, Debian, or openSUSE).
Set up Linux:
  • Launch your installed Linux distribution, create a user account, and set up your Linux environment.
Install Vi:
  • In the Linux terminal, you can install Vi (Vim) using the package manager, e.g. for Ubuntu:
1sudo apt update
2sudo apt install vim
3

2-Use Vi:

Now, you can use Vi within the WSL terminal as you would on a Unix-like system.

On macOS

macOS comes with Vi pre-installed, and you can access it through the Terminal. Follow these steps to get started:
Open Terminal:
  • You can find the Terminal application in the "Utilities" folder within the "Applications" folder.
Start Vi:
  • In the Terminal, type vi followed by the filename you want to edit. For example, to create or edit a file called "mytext.txt," type:
1vi mytext.txt
Use Vi:
  • Vi will open in the Terminal. You'll start in Normal mode. To enter Insert mode, press i.
  • To exit Insert mode and return to Normal mode, press Esc. To enter Command-line mode, press :.
Save and Quit:
  • To save your changes and quit Vi, press Esc, then type :wq and press Enter.
  • With these steps, you can start using Vi Editor on both Windows and macOS.
  • Remember that Vi can be challenging for beginners, so practice and familiarity will make it a more efficient text editor for your needs.