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.
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:
Enter normal mode by pressing the
ESC
keyType the colon
:
keyThe colon will appear in the command line as shown in the image below.
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
h
– move leftl
– move rightk
– move upj
– move downw
– move to the beginning of the next wordb
– move to the beginning of the previous worde
– 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
:w
– saves the document:q
– quits or exits vim editor:wq
– saves the document and quits the editor: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
dd
– this deletes the line in which the cursor is located.x
– this deletes the text on which the cursor is resting.p
– this pastes the deleted line of text on the next line after the cursora
– 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
Type
vim memo.txt
to create a new text document.Now, enter insert mode by pressing the
i
keyType this
Evrybody must learn how to proglam a compter because it teaches you how to tink.
Press the
ESC
key to enter normal mode.Save the document by typing
:w
Finally, correct the spelling mistakes. Move the cursor left and right by using the commands discussed above.
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.
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.