A Beginner's Guide to VIM
Table of contents
Introduction
Vim is renowned for its speed, efficiency, and versatility, making it an invaluable tool for developers and writers alike. Mastering Vim can supercharge your text editing workflow and revolutionize the way you work with text. In this blog post, we'll provide a beginner's guide to Vim, equipping you with the essentials to start using Vim and harness its full potential. Get ready to unlock a new level of productivity and efficiency with Vim!
Prerequisites
No significant prior knowledge is required; however, having a basic understanding of the Linux terminal is beneficial. If you're new to the terminal, consider exploring introductory resources. This will better equip you to make the most of this guide and enhance your learning experience. Let's get started on our journey to mastering Vim!
Definition and History of Vim
Vim, pronounced /vษชm/, is a text editor built by Bram Moolenaar and released to the public in 1991 as an improved clone of Vi. Vi is an older editor built by Bill Joy in 1976. During the 90s, Vi was lacking behind other editors like Emacs, so Bram implemented missing features to bridge the gap. Bram named the new editor Vim a contraction of Vi IMproved.
Vim is a free and open-source lightweight text editor with a wide range of features that can be used to edit files. Vim is highly customizable, allowing users to personalize the editor. Vim is available in Linux, Mac, and Windows. Though Vim traditionally exists only in CLIs, there are now GUI versions of the editor.
Installations and Configurations
Installation of Vim on Linux
- Run the command below to update your system's list of available packages.
$ sudo apt-get update
- Run the command below to install Vim.
$ sudo apt-get install vim
To ensure that Vim is correctly installed, execute the command below.
$ which vim
The output of the execution of the command above should be as shown below, displaying the location Vim is installed.
Installation on Windows Platform
Follow the simple steps below to install Vim on your Windows machine.
Visit the official Vim download website here vim.org.
Download the .exe installer compatible with your machine.
Double-click on the installer and follow the on-screen instructions to complete the installation.
Configurations
You can configure your Vim to be suitable for your needs. You can do this by creating and editing the .vimrc
file. The .vimrc
file is a configuration file located in the home directory. The .vimrc
file contains a set of commands that are used to configure Vim. These commands can be used to set the color scheme, the tab, the width, the syntax highlighting, and many other settings. Use the instructions below to number lines in Vim.
Navigate to your home directory.
$ cd ~
Create the
.vimrc
file.$ touch .vimrc
Open the file and write the desired command. For example, if we want to enable line numbers to ease file navigation. Write the command below in the
.vimrc
file to do just that.set number
For information on more configuration settings, click here.
Vim Modes
Vim is a modal editor, meaning that it has different modes for different tasks. The most common Vim modes are:
Normal mode: This is the default mode, and it is used for moving around the text and carrying out text editing operations like copy, cut, paste, etc. Press
Esc
to move from any mode to the Normal mode.Command-line mode: This mode is used for entering commands to Vim. Press
:
to enter the command-line mode from the Normal mode. The commands are shown in the bottom-left corner of the Vim interface.Insert mode: This mode is used for inserting text into the file. Press
i
to enter the insert mode from the Normal mode.Visual mode: This mode is used for selecting text. Press
v
to enter the Visual mode from the Normal mode.
Getting Started with Vim
Launching Vim
You can open Vim from the Command-line executing the command below.
$ vim
Which will display the Vim homepage below.
Creating a new file on Vim
You can create a new file on Vim by entering the command while in command-line mode, as shown in the image below. If the file
VIM_Tutorial
exists, the command will open it. If the file does not exist, it will create it.You can also open or create a new file directly from your terminal with the command below. If the file
VIM_Tutorial
exists, the command will open it. If the file does not exist, it will create it.$ vim VIM_Tutorial
Inserting Text
You have to switch to insert mode to start inserting text. You can do this by using any of the commands below.
Command | Function |
i | The i command is the most common way to switch to insert mode. It inserts text at the cursor's current position. |
I | This command will switch to insert mode at the beginning of the line. |
a | This command will switch to insert mode one position after the cursor's position. |
A | This command will switch to insert mode at the end of the line. |
o | This command will create a new line and switch to insert mode on that line. |
O | This command will create a new line before the current line and switch to insert mode on that line. |
Navigating the text on Vim
You can navigate text files easily using the key shortcuts listed in the table below. The key shortcuts work in normal mode except insert mode. However, in insert mode, you can navigate text files using the arrow keys.
Keyboard Shortcut | Function |
h | Move the cursor to the left by one position. |
l | Move the cursor to the right by one position. |
k | Move the cursor in an upward direction by one line. |
j | Move the cursor in a downward direction by one line. |
b | Move the cursor to the beginning of the previous word. |
e | Move the cursor to the end of the current word. |
w | Move the cursor to the beginning of the next word. |
You can also combine a number with commands in the table above in the format NumberCommand
to move the cursor by Number
position(s) along the direction of the Command
at a time. For example, you can use 15h
to move the cursor to the left by 15 positions.
Other Important Navigation Commands
Command | Function |
0 | Move the cursor to the beginning of the current line. |
$ | Move the cursor to the end of the current line. |
Ctrl + f | Scroll up the entire page. |
Ctrl + b | Scroll down the entire page. |
Text Editing Operations
Cut, Copy, and paste actions
Often you would need to perform cut, copy, and paste operations on text and Vim provides commands for these actions.
Command | Functions |
x | Cut or delete a character after the cursor position. |
X | Cut or delete a character before the cursor position. |
y | Copy or yank a character from the cursor position. |
p | Paste the character after the cursor position. |
P | Paste the character before the cursor position. |
dw | Delete one word from the cursor position. |
D | Delete the entire line from the cursor position. |
dd | Delete the entire line. |
Y | Copy the entire line. |
yy | Copy the entire line. |
You can also perform multi-line or multi-word operations combining a number with the command in the format NumberCommand
to move the cursor by Number
of times the effect of the Command
at a time. For example, you can use 15dw
to delete 15 words from the cursor position at a time.
Search
Search operations are very valuable in locating specific pieces of text within a document or codebase. In this part, we will cover the commands used in VIM to perform search operations.
Searching in the forward direction
/<search text>
After entering the search command, you can navigate through the search results using n
to move to the next occurrence and N
to move to the previous occurrence.
Searching in the backward direction
?<search text>
Also, you can navigate through the search results using N
to move to the next occurrence and n
to move to the previous occurrence.
Searching multiple files
To search through multiple files in Vim, use the :vimgrep
command. In the code snippet below, vimgrep
is utilized to search for the word 'pointers' in all text files.
: vimgrep pointers *.txt
In the same vein, you can navigate through the search results using :cn
to move to the next occurrence and :cN
to move to the previous occurrence.
Find and Replace
There are two common methods of performing search and replace in Vim.
Using Slash and Dot
This is the basic way of performing search and replace. This can be done by entering the /<search term>
. For example, if we want to search for the term functions
and replace it with subroutines
, we would enter the command below while in Vim normal mode.
/functions
Upon entering the command, Vim will promptly find the first occurrence of the word "functions," moving forward from the current cursor position. Simply press Enter to navigate directly to it. Now, you can effortlessly utilize the cgn
command to make changes as needed.
cgn
Vim will delete the word functions
currently under the cursor and put you in insert mode. You can then edit the text right away and press to apply the change.
However, searching for a word that appears multiple times with this method can become monotonous and time-consuming. To address this, an effective solution is introduced.
Using the Substitute Command
The Vim editor substitute command offers a solution for executing both simple and complex search and replace operations. The substitute command has the syntax below.
:s/<search_phrase>/<replace_phrase>/options
The command ":s" in stands for "substitute," and it allows us to search for a specific word, denoted by "search_phrase," and replace it with another word, represented by "replace_phrase." The options available include "c" for confirmation, "i" for ignoring the case, and "g" to replace all occurrences.
To search and replace all occurrences of functions
with subroutines
, we will use the command below.
:%s/functions/subroutines/g
Undo and Redo
Undo
In Vim, you can undo actions using simple commands enabling you to revert changes easily.
u
The u
command is used to undo single action. To undo a certain amount of actions at once, combine the number with the command. For example, 3u
will undo the last 3 actions. To undo all changes at once use the command.
U
Redo
Redo has the opposite effect to undo. It allows you to return to the state before using the undo command. You can perform a redo using any of the commands listed below.
Ctrl
+r
while in Normal mode.: red
while in the Command-line mode.
Saving and Exiting Vim
Exiting Vim can be tricky for beginners. This table introduces the different commands used to exit Vim and explains when to use them.
Command | Function |
: w | Save changes to the file. |
: wq | Save all changes to the file before exiting Vim. |
: q! | Exit Vim without saving changes made to the file. |
: qa! | Exit all open files without saving changes made to them. |
Buffer and Swap Space
Buffer
A buffer is a temporary storage area for text in Vim. When you open a file in Vim, the file's contents are loaded into a buffer. The buffer is stored in RAM, which is the computer's short-term memory. This means that the file's contents are only available while Vim is running. When you edit a file in Vim, you are editing the contents of the buffer. Any changes you make to the buffer are not saved to the file until you save the file. When you save a file, Vim writes the contents of the buffer back to the file on disk. This ensures that your changes are saved permanently.
Swap Space
A swap file is a temporary file that Vim creates to store the contents of a buffer. Vim creates a swap file whenever you open a file or make a change to a file. Vim copies the buffer content to the file only when you save the buffer. However, Vim periodically copies the buffer content to a swap file. This is done to ensure that you can recover your changes in case of a crash or power outage. The swap file is created in the same directory as the file you are editing. The name of the swap file is the same as the name of the file, with a .swp
extension.
If Vim crashes or you lose power while you are editing a file, you can use the :recover
command to recover your changes from the swap file. The :recover
command will open the swap file and restore the contents of the buffer to the file.
Vim Help System
There are several ways to access the Vim help system. One common way is by using the command below while in command-line mode. The : help
command displays the entire help manual about Vim.
: help
To get help on a particular topic, you can extend the : help
command in the format : help <specific topic>
. The example below uses the command to get help with buffer.
: help buffer
But there are times you might not know the exact topic. In this cases, you can use the : helpgrep <phrases
command to access help.
: helpgrep
Conclusion
In conclusion, Vim is a powerful and versatile text editor, that offers an efficient way to edit files. Don't worry about memorizing all commands; pick up what you need and grow as you use Vim. Whether you're a coding enthusiast or a writer, embrace Vim's unique approach to text editing.
Explore more tech tips and tricks on my blog! Happy Vimming! ๐๐
Subscribe to my newsletter
Read articles from Abdulhakeem Mayaki directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Abdulhakeem Mayaki
Abdulhakeem Mayaki
Hey there! I'm a passionate web developer and tech enthusiast. ๐ป I'm on a journey to explore the ever-evolving world of web technologies, and I love sharing my insights and knowledge with the community. ๐ Whether it's coding tips, web development tutorials, or insights into the latest tech trends, you'll find it all here. Join me on this exciting tech adventure! ๐ #WebDev #TechEnthusiast #CodingJourney