A Quick Introduction to Editing with Vi / Vim

vi Mar 28, 2021

Learning the Vi / Vim editor will unleash new powers to the toolbox that you already have as a computer user. Just so you know... Vi is pronounced as Vee-eye (Just like the letter 'V' and the letter 'i') and is supposed to be short for Visual editor.

Most people are intimated by the command line because at most we have been spoiled by the icons and the beautiful interfaces brought up by the GUI (Graphical User Interface). If you are completely unfamiliar with the command line, please take a look at my blog post: A Friendly Approach to Start Using the Command Line (PowerShell) on Windows

Okay, today I am going to introduce you to something new. This is called Vi or Vim. Vim is a text editor that is available on Unix platforms like Linux and Mac OS that allows you to edit text.

If you're wondering why should you use Vim while other text editors are available, it's because with Vim you learn how to have more control and to be more productive as you will only be working from your keyboard -typing commands. You won't be navigating using your trackpad/touchpad or mouse to different angles of your screen something that wastes our precious time at times.

I might not have succeeded in selling Vim to you yet, but please follow along and see what this editor can do.

What is Vi / Vim?

Vi has been the standard editor for Unix and Linux systems for so many years. With Vi, you can create new files or edit any text files.

If you have ever tried to host your website using Digital Ocean you'll find out you need to edit those files on the server. And one of the easiest ways to do that is to use Vi editor or some variation of it.

Get Started with Vi

I am going to share with you simple Vi commands which can get you started with editing with Vi. Practice is the name of the game, with practice these commands should come as second nature. So use these commands a lot till they become second nature then you can advance to even more and powerful features of Vi.

This tutorial is short but these commands should provide you with some basic editing skills with Vi for you to get started.

Open Console

You can open Vi editor by just typing vi

vi
vi command to open Vi editor

See photo below, From my terminal window, on my root folder ([email protected]:/#) and I just typed vi

vi command on terminal

After typing the command the following window opens:

First time you open Vi editor

The first time you open Vi editor you should see that screen, depending on which version you use you might not see that exact screen. As you can see from that screen the version of Vi I use is called Vim...that really doesn't matter all these commands should be the same in whatever Vi you have.

At the very top, you should see a blinking cursor. You can start typing commands now. BUT before typing you should note that Vi editor works on two modes:

  1. Insert Mode
  2. Command Mode

Insert Mode

In this mode, the text you type becomes part of the document (everything typed becomes text in your file). The first time you open Vi editor, it won't be in this mode, so to activate the Insert Mode you should hit letter i on your keyboard. If you try and hit i the second time you will see that character written on the editor because you're already in the Insert mode.

Command Mode

The default mode of Vi editor, as soon as you open the Vi editor it will be in this mode and in here you can start typing your commands.

Already we have learned our first command after open the Vi editor and that command was just letter i to change from command mode to insert mode.

When in insert mode you can change back to command mode by hitting the ESC key on your keyboard.

Please Note: Without knowing you can accidentally enter or leave insert mode. Press the ESC key to enter command mode. If you were already in command mode and you enter the ESC key you will hear a beep sound. And because of that command mode is sometimes referred to as the beep mode.

Ok, we now know how to open the Vi editor and switch between the Insert and Command Mode.

Vi Editor - More Commands

Before we start learning about more Vi commands you should note that Vi commands are case-sensitive so the i (lowercase letter) is different from the I (uppercase letter)

Also, you should note that you won't be seeing the commands that you type. And after typing the command you don't need to hit ENTER on your keyboard.

Opening a File

Just like in a normal text editor like Word or Notepad where you can either choose to name a file first and then open the application OR you can choose to open the application and name a file letter the same can be done with the Vi editor.

vi - this is the command that you can use to open the Vi editor, write your text and then save the document. Open in this way means you will have an unnamed file. Alternatively, you can use the command as follows:

vi filename - this will open the Vi editor and name the specific file. For example, vi practice will assign the name 'practice' to your file before you start editing it.

Let's do an example of this. From my terminal window:

Command to open Vi editor with a named file
vi practice
To open a new file named practice on Vi Editor

Upon opening that you should see a blank screen like this:

Vi Editor after opening a new file

At the very top you see a blinking cursor...then followed by tilde (~) symbols. The tilde symbols just indicate that there is no text in the file (not even blank lines).

At the very bottom, you will see the status line or sometimes referred to as the prompt line that shows the name of the file [practice] and the status of the file [New File]

Let's Practice

Now you have Vi editor hit letter i on your keyboard to go to the Insert mode that will allow us to start working with text.

After you hit letter i, you should see the status line changing to INSERT, indicating that we are on Insert mode.

Command i to enter insert mode

Now I will start typing:

I have typed on the first line

Now it works just like a normal text editor, I can hit Enter to go on the second line, third line, etc.

Typing on line 2 and line 3

You see how easy it is!

Now let's save the file we just created.

Saving a File

To save a file on Vi editor we are going to use the command ZZ (Capital letter ZZ). ZZ command allows you to save your edits and return back to the terminal window.

Now remember that we're still on Insert mode, so to be able to use the command ZZ we need to go to the Command mode and we do that by hitting the ESC key.

After hitting the ESC key, then we type ZZ and back we are at the terminal.

Another way to save a file in Vim without exiting the editor is the command :w

In a Nutshell

The only way to get comfortable with the Vi editor is by practicing. The more you practice using the editor the more they become second nature.

Practice:

In this practice...we're going to open the Vi editor by creating a new file, doing small edits, save and then quit the file.

  1. From your terminal window type the following
vi testfile

This will create and open a file called testfile from the directory we are working from. Then immediately we will be inside the Vi editor.

2. Insert new text

From the Vi editor first thing we have to do is to make we are in the Insert mode by hitting letter i on your keyboard. After hitting i start typing any text that you want just as you would do in a normal text editor.

3. We're done editing - Let's Go back to Command Mode

Now we are satisfied with our edits we need to go back to the command mode by hitting ESC on your keyboard.

4. Now we're on command mode we will hit letters ZZ to quit Vi editor and save the edits we have made.

5. If you want to open that file again just type:

vi testfile

Thanks for following this short introduction to the Vi editor. Next time we will learn about more commands and improve our productivity when editing text.

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.