Vim is one of the most popular text editors among Linux users. Linux System Administrators especially often prefer it to other editors.
In this article, you'll learn a lot about Vim and see how you can quickly start using Vim as a developer.
What is Vim?
Vim is an acronym for Vi IMproved. It is a free and open-source cross-platform text editor. It was first released by Bram Moolenaar in 1991 for UNIX variants.
Vim is based on the original Vi editor, which was created by Bill Joy in 1976. In the 90’s, it started becoming clear that Vi was lacking in some features when compared with the Emacs editor. So Bram implemented many missing features and released it under the name Vim.
How to Install Vim
Vim runs across various platforms such as Windows, Linux, and Mac.
To install Vim on Windows, download the executable file from the Vim site and run the file. Follow the instructions shown on the screen and you'll be good to go.
Vim comes pre-installed on most *nix operating systems. But if it's installed on your system, you can install it with a package manager of your choice.
Here's the installation command for Debian-based operating systems:
sudo apt-get update
sudo apt-get install vim
To ensure that it's installed properly, run which vim
and you should get /usr/bin/vim
in your output.
How to Get Started with Vim
You can get started with Vim by typing its name on the terminal like this:

Once you enter the above command, you'll be able to see a screen displaying info about Vim and some instruction to find help and exit Vim.

Vim Modes
You should be aware of the most important concept in Vim before moving on: modes in Vim.
Everything in Vim is considered a mode. You can achieve whatever you want if you understand modes in Vim. There are many modes in Vim. But, we'll be looking at the 4 most important modes.
They are:
- Command Mode
- Command-Line Mode
- Insert Mode
- Visual Mode
Let's explore them one by one.
What is Command Mode?
This is the default mode (also called Normal mode) in Vim. Whenever Vim starts, you'll be in this mode. You can switch to any mode from this mode. You can't do this in any other modes.
Basically, to switch from one mode to another, you have to come to Command Mode first and then navigate to the other mode. The commands that you run without any prefix (colon) indicate that you're running the command in command mode.
What is Insert Mode?
This mode is used to edit the contents of the file. You can switch to insert mode by pressing i
from command mode. You can use the Esc
key to switch back to command mode.
What is Command Line Mode?
You can use this mode to play around with some commands. But the commands in this mode are prefixed with a colon (:). You can switch to this mode by pressing : (colon) in command mode.
What is Visual Mode?
You use this mode to visually select some text and run commands over that section of code. You can switch to this mode by pressing v
from the command mode.
The above 4 modes are enough to perform a basic set of file operations in Vim.
Ok the theory is done. Let's explore Vim practically.
Common Text Editor Operations in Vim
How to Create a New File
Creating a new file with Vim is simple. You can do it from command line mode.
Run the following command to create a new file:
:edit sample.txt
The above command opens a sample.txt
file in edit mode if it exists, and creates a new file otherwise.

sample.txt
After running that command, you'll be in command mode (as shown in the below screenshot), and you'll not be able to enter any text:

To add some text to the created file, press i
in the keyboard. You use the i
command to enter some text in the file. Once you press i
, you'll be able to see that you entered Insert mode in Vim by looking at the bottom left of the file.

sample.txt
file in VimIn this mode, you can type whatever you want into the file.

sample.txt
fileWe're done with writing our content. And now we'll want to save the file. If you do not save and just close the terminal at this point, all your content will be lost.
How to Save a File
To save a file, you have to switch from Insert mode to Command Line mode. Remember I told you earlier – whenever you want to switch from one mode to another, you have to first switch to Command mode and then you can easily switch to whatever mode you want.
To switch to Command mode from Insert mode, you have to press the Esc
key.
After you press the Esc
key, you'll not be able to see the --INSERT--
at the bottom left. This indicates that you're not in Insert mode anymore and you're now in Command mode.

sample.txt
fileTo save the file, type the following command:
:w

How to Close a File and Exit Vim
Once you save the file, you can close Vim by closing the terminal. But, the proper way to close the file and the Vim editor is by using the following command:
:q

The above command closes the file and quits the vim editor.
Alternatively, you can use a command that's the combination of the above 2 commands (save & quit) to quickly save and quit Vim. The command is:
:wq
The above command quits the Vim editor immediately after saving the file.
How to Edit a File in Vim
To edit a file, you have to open the file using Vim and switch to Insert mode.
Let's open the sample.txt
file we created above:
vim sample.txt
sample.txt
file
sample.txt
file using VimWe are in Command mode now. To edit the file, we have to switch to Insert mode. As we saw earlier, pressing i
from the Command mode will switch to Insert mode.

i
Follow the same procedure for saving the file and quitting Vim. Press Esc
on the keyboard and type :w
to save the file and :q
to quit Vim.
You may wonder, though, what if I want to close the file without saving my changes? (Ignore the changes I made and bring the file back to the old state).
How to close the file without saving the changes
To close the file without saving the changes, you have to run :q!
from Command mode.
Let's explore this with an example:

sample.txt
fileI've added some more content to the file.

sample.txt
fileBut, I don't want to save the changes I made now. To close the file without saving this change, we have to switch to Command mode (by pressing the Esc
key). Type :q!
in the Command mode. This will close the file ignoring the changes made.

sample.txt
fileLet's view the file and confirm if we get the expected output:

sample.txt
fileYeah. The file does not have the last line which we added recently. So, the changes were not saved.
How to Cut, Copy, and Paste Text from a File using Vim
You can Cut, Copy, and Paste in 2 ways in Vim. They are:
- Using Visual mode
- Using keyboard
Out of these two ways, Visual mode is simpler to understand. Since this is a beginner-friendly guide, let's explore how to cut, copy, and paste in Visual mode.
Let's open the file in command mode before we proceed:

sample.txt
file opened in command mode in VimLet's assume you want to copy the word "Hello" from the 1st line and paste it into the 3rd line.
The first step is to place your cursor in the place from where you want to copy the text (being in command mode).

Hello
Enter Visual mode by pressing the v
key on the keyboard.

The -- VISUAL –-
text at the bottom left indicates that we're in visual mode.
Move the cursor to the place where the text you want to copy ends.
In this case, I'm moving the cursor to the letter o
of the word Hello
.
While you move your cursor, Visual mode highlights the text from the beginning up to your cursor.

Once you're done moving your cursor to the right place, hit y
to copy the text or hit d
to cut the text.
In this case, I'm copying the text. So, I press y
on my keyboard.
Move the cursor to the place where you want to paste the text.

In our case, we have to move the cursor to the 3rd line.
Press p
(in lowercase) to paste the text after the cursor and P
(in uppercase) to paste the text before the cursor.

Hello
text pasted on 3rd linePress :wq
to save and close the file
How to Find and Replace Text in Vim
Finding text and replacing it with some other text is simple and straightforward in Vim. There's a one-line command that simplifies this entire process.
Here's the syntax:
:[range]s/{pattern}/{string}/[flags]
Let's dismantle each part and understand how it all works.
[range]
indicates that you can pass the range of lines. Pass % to find and replace in all lines. The range is separated by a comma. To find and replace between lines 5 to 10, pass 5,10. Use.
to represent the current line and$
the last line of the file.{pattern}
indicates the pattern to find the text. You can pass regex patterns here.{string}
is the string to replace in the found text.[flags]
indicates if you wish to pass any additional flags (for example, the flagc
is passed to confirm before replacing the text). By default, this does a case-sensitive search. You can change it to do a case-insensitive search by passing ai
flag.
Alright, let's explore this with an example.

sample.txt
file contentOur sample.txt
file has 2 "Hello"s. Let's replace "Hello" with "Hi" at both places.
The command to do that is:
:%s/Hello/Hi/g
%s
indicates replacing the content in the entire fileHello
is the search textHi
is the text to replaceg
indicates making the change globally


Let's understand this with another example.
This time, I want to change the word "Hi" (case-insensitive search) that occurs between lines 2 and 3 and replace it with "Hello and Welcome", with a confirmation to change each occurrence.
The command to do that is:
:2,3s/Hi/Hello and Welcome/gci


Here's a description for each option:
y
- Replace the matchn
- Skip the matcha
- Substitutes the match and all remaining occurrences of the matchq
orEsc
- Quit substitutionl
- Replace the match and quitCTRL+Y
- Scroll the screen downCTRL+E
- Scroll the screen up
I want to accept the change. So, I press y
. Here's the output after pressing y
.

Since we have only one occurrence of "Hi" between lines 2 and 3, it did not ask for any further confirmation and the operation is complete.
How to Undo or Redo in Vim
To undo a change in Vim, press u
in command mode. To redo, press CTRL + R
. You can prepend a number (n) with u
to undo n
times. for example, 2u
will undo 2 times. To list the available undo options, type :undolist
in command mode.
Let's understand this with an example.
Here's the current state of our sample.txt file:

sample.txt
fileI have my cursor at "a" in the "Hello and Welcome" text on 3rd line. Let's remove the word "and" by typing dw
on command mode:

and
by typing dw
in command modeLet's undo to bring the word "and" back into the file. To do so, press u
in command mode.

u
in command modeLet's redo and take away the word "and" by typing CTRL + R
in command mode.

CTRL + R
in command modeConclusion
In this article, you have learned the basics of Vim. This article should be enough to get you going and do some basic file read/write operations with Vim.
Just keep in mind that I've not even covered 1% of Vim. But I'm sure these basics will help you explore Vim quickly and confidently.
To learn more about Vim, subscribe to my email newsletter at my site and follow me on social media.