At some point every Linux user has to modify a file and what better editor to do it than vim, its commands allow you to do pretty much anything you want.
What is Vim in Linux?
Vim (that stands for Vi iMproved) is the main text editor available on the Linux Command Line Interface (CLI) to create and edit files. It provides two main modes: the command mode and the insert mode.
If you are new to vim you might find switching between modes not always straightforward and if you have already used vim for long time there is always the opportunity to learn new commands to speed up your editing.
In this article we will start with basic vim commands and gradually move to more complex ones that I have personally used over the years.
Let’s get started!
Vim Command Mode and Insert Mode
First of all, we will start by creating an empty file called app.conf. We will assume this file contains the configuration for an application running on Linux:
With this command we open the vim editor and considering that this is a new file you will not see anything in it.
So, how do we start adding lines to our file?
If you try to type letters on your keyboard you will notice that nothing appears on the screen (unless you type some letters that represent special Vim commands).
The reason why you are not seeing anything in the vim editor is because you are in command mode.
The Vim command mode is designed to move quickly around a text file and to perform functions like copying words, copying and deleting one or more lines, searching, replacing and a lot more.
To start adding text to our file we have to switch to insert mode. To do that simply press the letter ‘i’ on your keyboard and write whatever you want:
This is a vim tutorial ~ ~ ~ -- INSERT --
As you can see when I enter insert mode in vim at the bottom of the screen I see — INSERT —.
And how do I save the file?
First I have to exit insert mode and enter command mode using ESC. Once that’s done, — INSERT — at the bottom disappears.
Vim Basic Commands
At this point we have few options to save the content of the file. While being in command mode simply type the following on your keyboard. They will appear at the bottom of your vim window.
Press ENTER to execute the command:
|Save the file without exiting vim|
|Save the file and exit vim|
|Save the file and exit vim|
What if we want to exit without saving the content of our file?
We can use :q in command mode.
Let’s what happens when we do that and press ENTER with our file…
This is a vim tutorial ~ ~ ~ E37: No write since last change (add ! to override)
We see an error at the bottom of the terminal that warns us about the fact that the file has been changed and we are not saving the changes.
To confirm this is definitely what we want to do we have to add an exclamation mark at the end of our command.
So, the command becomes :q!
This is a vim tutorial ~ ~ ~ :q!
After pressing ENTER you will go back to the Linux shell without saving the file.
As you will see vim provides lots of different keyboard shortcuts to edit your files. In the next section we will see how to move around a file.
How to Use Vim to Move Around Your File
One of the great advantages of editing a file with vim is having shortcuts to move around your file quickly.
For example, let’s say I want to jump to a line in a file...
To jump to a line in vim use : (colon) followed by the line number.
Below you can see how I can go to the third line of my file using :3.
First line Second line Third line Fourth line ~ ~ ~ :3
After pressing ENTER you will see your cursor at the beginning of the third line.
This can be very useful if you know that there is a syntax error in one of your configuration files at a specific line and you want to jump to that line to understand where the error is.
The following table shows some commands you will find useful to move around your file:
|:0||Move to the beginning of the file (line zero)|
|:$||Move to the end of the file|
|:n||Move to line n|
|0||Move to the beginning of a line|
|^||Move to the first non-blank character in a line|
|$||Move to the end of a line|
|H||Move to top of the screen|
|M||Move to middle of the screen|
|L||Move to bottom of the screen|
|b||Move to the previous word|
|w||Move to the next word|
I think I have given you enough options to move quickly around your files in vim 🙂
Try each one of them before continuing with the next section. This will help you remember all the commands by the time you finish reading this article.
They can be very handy when you have to be fast in editing your files!
Insert, Copy and Delete Lines
One of the main things you will find yourself doing when editing a file is adding new lines.
A quick way to insert a new line using vim is by pressing the letter ‘o’ while being in command mode.
Doing that will automatically create an empty line under the current line, move the cursor to the beginning of the new empty line and switch vim into insert mode to allow you to write text.
Another common operation is copying lines, often because we need to add lines that are slightly similar to an existing one and just change few words.
Below you can see an example where we copy the following line and change only part of it:
auth.endpoint=/api/auth/v1 ~ ~ ~ ~ "vim_tutorial.txt" 1L, 27C
Firstly, copy the line and paste its copy below it. To do that:
- Go anywhere in the first line in command mode.
- Press yy to copy the line (yank to clipboard).
- Press p to paste the line below.
auth.endpoint=/api/auth/v1 auth.endpoint=/api/auth/v1 ~ ~ ~ ~ "vim_tutorial.txt" 2L, 54C written
Now, switch to insert mode and apply the following changes to the second line:
auth.endpoint=/api/auth/v1 analytics.endpoint=/api/analytics/v1 ~ ~ ~ ~ "vim_tutorial.txt" 2L, 64C written
Then, let’s say you want to delete a line…
Just move the cursor somewhere on that line and press dd. The line will disappear
How to Copy, Paste and Delete Multiple Lines
Sometimes you may have the need to copy and paste multiple lines in a file.
In the previous section we have seen how to use yy and p to do that for a single line.
To do that with two, three, four lines, etc… you can:
- Move the cursor to the first line of the block of lines you want to copy.
- Type 2yy or 3yy or 4yy, etc… depending on the number of lines you want to copy.
- Move to the last line of the block and press ‘p’.
This will paste all the lines we have copied, immediately after the first block of lines.
The same applies to deleting multiple lines…
For example, to delete two, three, four lines use the command 2dd, 3dd and 4dd, etc…
Give it a try before continuing.
Searching For Text in a File
I can’t count the number of times I had to search for text in a file while editing it in vim.
As you might know the grep command can be used to search for text in a file too.
So why doing the same thing with vim?
You would usually be searching for text in a file using vim if you want to find and modify the text immediately in the editor.
Grep, on the other side, is mainly used to find which files contain a specific pattern.
Let’s say, for example, that you want to modify the value of a property somewhere in a long configuration file.
To quickly move to the place in the file where the text I’m looking for is, I can use the forward slash in command mode.
As an example I will open the configuration file for the SSH daemon in Linux. I want to search for AuthorizedKeysFile in the following file:
After opening the file in vim I will be in command mode and the cursor will be at the beginning of the first line.
To search for the specific text I can simply type a forward slash ( / ) followed by AuthorizedKeysFile.
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin .... .... [ more lines in the file] .... .... /AuthorizedKeysFile
When you press Enter the cursor will jump to the first occurrence of the text I’m searching for.
Also, in case there are multiple occurrences of the same text in the file, you can use the letter n (still in command mode) to jump to the next occurrence of the text in the file.
How to Copy and Delete Words
Sometimes you might want to copy and paste a long string to different places in your file.
To do that move the cursor to the beginning of the word you want to copy and make sure you are in command mode.
Then type yiw to copy the word you are interested in, move wherever you want to paste the word and press p (still while being in command mode).
In a similar way you can use diw to delete a word after moving the cursor to the beginning of the word you want to delete.
Some More Useful Vim Tricks
Let’s have a look at some tricks that can be very useful when working with text files in vim.
- Add line numbers to Vim: in command mode type :set number and press Enter. To hide line numbers type :set nonumber.
- Highlight syntax: to enable syntax highlighting type :syntax on followed by Enter (in command mode). To hide colours that highlight syntax use :syntax off.
- Redo or repeat the last command: to repeat a command you can use the dot (.) in command mode. For example try to copy paste a line using yy followed by p and then press the dot, see what happens.
- Undo a change: This is a VERY USEFUL command! If you make a change to your file and you realised the change is incorrect, you can simply undo the change by pressing the letter ‘u’ in command mode.
- Move a line down and up: place the cursor on the line you want to move. To move it down type :m +1. To move the same line up type :m -2.
- Convert line into uppercase: use the gUU command.
- Convert line into lowercase: use the guu command.
- Count number of words: to count the number of words in a text file using vim type g followed by Ctrl-g. You will see a similar output to the one below at the bottom of the vim screen:
Col 7 of 45; Line 3 of 3; Word 22 of 30; Byte 98 of 137
In this specific file we have 30 words and the cursor is currently at word 22.
Find and Replace
We have already seen how to find text in a file using vim.
Something I have been using a lot over the years is a command that allows to search for specific text and replace it with different text.
When could this be required?
For example, if you want to replace all the occurrences of a string in a file using a single command. It’s very common in configuration files.
Imagine you have the following file:
This is a TO_BE_REPLACED tutorial. TO_BE_REPLACED is a Linux text editor. In this tutorial you will learn TO_BE_REPLACED commands. TO_BE_REPLACED is a very powerful editor.
And you want to replace the text TO_BE_REPLACED with the word ‘vim’.
The following command can be used as a starting point:
Let’s see what it does when applied to our text file while keeping the cursor on the first line:
After pressing Enter the content of the file becomes:
This is a vim tutorial. TO_BE_REPLACED is a Linux text editor. In this tutorial you will learn TO_BE_REPLACED commands. TO_BE_REPLACED is a very powerful editor.
As you can see the only text that has been replaced is the first occurrence of TO_BE_REPLACED in the first line.
How can we replace all the occurrences in the first line?
With the following command…
The difference compared to the previous command is the letter g at the end that applies the replace command to the entire line and not just to the first occurrence of the text we want to replace.
But, this doesn’t solve the full problem.
I still want to be able to replace TO_BE_REPLACED in every single line of my file.
To do that I can add 1,$ after the colon in the command. This expression applies the change to the entire file because 1 stands for line 1 and $ stands for the end of the file.
Use the following command and confirm the file changes in the way we expect:
Give it a try! Does it work?
I must have used this command thousands of times in my career!
How To Open Multiple Files in Vim
It can help being able to open two files in the same vim window when you need to check the information in one file to make some changes in a different file.
Let’s see how it works…
Firstly, I edit a file called vim_file1.txt using the following command:
Then with vim in command mode I can use the command :vsplit to split the screen vertically into two sections side by side.
At this point both sections show the original file and to move to the right section use Ctrl+w followed by the right arrow.
Then to open a different file in the right section use :e filename. So in our case, e: vim_file2.txt.
You will see the content of the second file appear in the right section:
The vim editor is very powerful!
We have covered so many different commands in this tutorial. Go through each one of them to make sure you remember them when you will need them.
It’s easy to forget something we don’t use.
Finally, a quick recap of what we have learned:
- The basic vim modes: command and insert mode.
- How to modify and save changes applied to a file.
- The best way to quickly move around a text file.
- The way to insert, copy and delete lines.
- How to find and replace text fast.
- That vim allows to split the screen and open multiple files at the same time.
- A lot more vim tricks.
And now you have the knowledge to be the vim guru of your team! 😀
I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!