Say Hello to Vi: A Beginner's Guide

Say Hello to Vi: A Beginner's Guide

Hello there! Are you new to the world of Linux and curious about Vi, but unsure where to start? Well, this blog is for you! Vi is a powerful text editor that has been around for over 40 years. Despite its age, it remains one of the most popular text editors. This beginner's guide will walk you through the basics of Vi, including essential commands that will help you navigate and edit text files like a pro. By the end of this guide, you'll have a solid foundation in Vi and be able to use it with confidence. So let's get started!

I. Getting Started with Vi:

Vi is a built-in text editor that is included in almost every Linux distribution. It was created by Bill joy in 1976. It has two modes of operation command mode and insert mode.

  • Command mode is the default mode of Vi. It allows us to navigate and perform various editing commands. In this mode, every key pressed is interpreted as a command.

  • Insert mode allows us to insert and edit text. In this mode, every key pressed is interpreted as a character to be inserted into the file.

Pressing the "Esc" key will let us switch from the insert mode to the command mode while pressing the "i" key will let us switch from the command mode to the insert mode.

To install Vi on an Ubuntu operating system, we can use the following command in the terminal:

sudo apt-get install vim

Note: Vim is an enhanced version of the original Vi editor (Vi improved).

II. Essential Vi Commands:

Opening and Creating Files in Vi:

To open a file using Vi, we simply type vi followed by the file name. If the file exists, Vi will open it for editing. If it does not exist Vi will create a new file with that name.

vi filename

Exiting and Saving Files in Vi:

There are two ways to exit a file in Vi:

  • Firstly, we can quit without saving the changes, by typing ":q!" in the command mode and pressing enter.

  • Secondly, we can save the changes before quitting by typing ":wq" in the command mode and pressing enter.

Deleting in Vi:

  • Deleting a character: To delete a single character, we move the cursor using the arrow keys to the character that we want to delete, then we press the "x" key.
  • Deleting a word: To delete a word, we move the cursor to the beginning of the word, then we type "dw". The "dw" command stands for "delete Word".

Cutting and Pasting Lines in Vi:

  • To cut a line, place the cursor anywhere on the line and type "dd". This command will remove the entire line and store it in the buffer for later use.

  • To paste the line that was just cut, we type the character "p".

Coping and Pasting Lines in Vi:

  • To copy a line, place the cursor anywhere on the line and type "yy". This command will copy the entire line and store it in the buffer for later use.

  • To paste the line that was just copied, we type the character "p".

Undoing changes in a file in Vi:

In Vi, we can undo changes in a file by typing the "u" character when we are in command mode.

Searching Forward and Backward in Vi:

  • To search forward for a pattern we use the command "/pattern".

  • To search backward for a pattern we use the command "?pattern".

Navigating to the beginning and end of a line:

  • To navigate to the beginning of a line, we type the character "0".

  • To navigate to the end of a line, we type the character "$".

In conclusion, Vi is a powerful and valuable text editor. It is widely used by programmers and Linux users due to its speed, efficiency, and flexibility. In this beginner's guide, we covered the basics of Vi, including how to navigate and edit text files, cut and paste lines, search forward and backward, and undo changes. With these essential commands, you'll be able to use Vi like a pro in no time. If you're interested in exploring another popular text editor, be sure to check out my next blog which will be about Emacs.

References:

Basic vi Commands. (n.d.). Colorado State University Department of Computer Science. Retrieved from https://www.cs.colostate.edu/helpdocs/vi.html