Getting Started with Vim

Introduction

This post is the continuation of the previous post, How to Code with Your Phone. I encourage you to read that post before this if you haven’t. The main focus of this post is using Tim in the Termux Terminal Emulator on a mobile phone. However, the ideas discussed apply to using Vim on a desktop or laptop computer.

What is Vim?

Vim is a very powerful text editor that is popular among developers. It is the default editor of the UNIX operating system. It stands for Visual Editor Improved. This editor is known for its steep learning curve. Nonetheless, it boosts productivity significantly once it has been mastered. This post will introduce you to the basics of the editor.

Modes in Vim

Unlike the other editors, vim comes with several modes of operation. Each mode specifies what vim does. We will focus on three main modes as you will be using these modes frequently when using Vim.

Normal Mode (ESC)

This is the default mode of the editor. This mode is used for moving through the document and making some edits. This mode is activated by pressing the ESC key. The image below shows this mode when a document is opened for the first time.

Vim in normal mode

Insert Mode (i)

This mode enables you to type text into the opened document. It can be activated by pressing the letter i on the keypad. As shown in the image below, the word --INSERT-- appears in the command line when the i key is pressed. Also, a cursor appears in the document area (top left corner). To exit this mode, press the ESC key. This will return Vim to Normal Mode**.**

Command-line Mode (:)

This mode, when activated enables you to enter various commands which vim executes on the document. These commands are for manipulating the document such as saving, deleting, copying, pasting, discarding changes etc. To enter this mode:

  1. Enter normal mode by pressing the ESC key

  2. Type the colon : key

  3. The colon will appear in the command line as shown in the image below.

  4. You can then add other commands to the column to execute.

To exit this mode, press the ESC key on the keypad. The following shows the command-line mode

Basic Navigation

It is unfortunate clicking or tapping to move the cursor does not work in Vim. You can however move the cursor through the texts in Normal Mode by using the following commands

  1. h – move left

  2. l – move right

  3. k – move up

  4. j – move down

  5. w – move to the beginning of the next word

  6. b – move to the beginning of the previous word

  7. e – move to the end of the current word

Additionally, you can also scroll up and down to move the cursor only in normal mode. Those using computers can also use the arrow keys to move the cursor in any mode.

Manipulation Commands

Command-line Mode

These commands are entered in command-line mode to execute on the document

  1. :w – saves the document

  2. :q – quits or exits vim editor

  3. :wq – saves the document and quits the editor

  4. :q! - quits the editor without saving the document. Any changes made will be discarded

Normal Mode

These commands are executed in Normal Mode. You don’t need to be in command-line mode as they are executed on the document

  1. dd – this deletes the line in which the cursor is located.

  2. x – this deletes the text on which the cursor is resting.

  3. p – this pastes the deleted line of text on the next line after the cursor

  4. a – this enters insert mode

Putting It All Together

The commands we have covered in this post are enough to get you started creating and manipulating documents using the Vim editor. To practice what we have learned, we will use a few of the commands to create and edit a document. Exit Vim by pressing ESC , then :q! Press the enter key and follow these steps

  1. Type vim memo.txt to create a new text document.

  2. Now, enter insert mode by pressing the i key

  3. Type this

    Evrybody must learn how to proglam a compter because it teaches you how to tink.

  4. Press the ESC key to enter normal mode.

  5. Save the document by typing :w

  6. Finally, correct the spelling mistakes. Move the cursor left and right by using the commands discussed above.

  7. When done, save the document and exit vim: ESC, :wq

Well done in reaching the end of this tutorial. We have learned some basic commands to edit documents using the vim editor. These are just the basics; I encourage you to explore and practice more.

Have questions or suggestions, let me know your thoughts in the comments.

1
Subscribe to my newsletter

Read articles from Emmanuel Enchill directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Emmanuel Enchill
Emmanuel Enchill

I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.