Learn Linux: An Introduction to the VIM Text Editor
Minicourse: Linux Command Line for Beginners | Lesson 3
Welcome back to our Minicourse: The Linux Command Line for Beginners! In the previous lesson, we learned about navigating the file system using commands like tree
, ls
, cd
, and pwd
. We also explored how to create files using the touch
command. In this lesson, we will focus on editing files using a powerful command line text editor called Vim.
Understanding Vim
Vim, short for "Vi Improved," is a text editor that comes pre-installed on most Linux distributions. It may seem a bit intimidating at first, but with practice, it can become a valuable tool for editing files efficiently.
Creating a New File with Touch Before we dive into Vim
Let's quickly review how to create a new file using the touch
command. To create a file named "myfile.txt," you can run the following command:
touch myfile.txt
This will create an empty file named myfile.txt in your current directory.
Opening a File with Vim
Now that we have our file, let's open it in Vim to start editing. To open the file in Vim, simply run the following command:
vim myfile.txt
After running this command, you'll enter Vim's default mode called "Normal" mode. In this mode, you can navigate through the file and perform various editing operations.
What to do if VIM is not Installed
If for some reason vim
is not installed on your system, and you have access to apt
you can quickly download it by running:
sudo apt update && sudo apt install vim
Note: you will need to enter your password (it won’t show as your type) and press enter for the sudo
. More on that in future lessons. Just know that if you are asked for your password and you begin typing and nothing shows up in the terminal, that is expected. You just need to type your password and press Enter
even though it seems like you are not able to type.
Inserting and Editing Text in Vim
In Vim's Normal mode, you cannot directly edit the text. Instead, you need to switch to the "Insert" mode. To enter the Insert mode and start inserting text at the cursor position, press the i
key. You will notice the word "INSERT" appear at the bottom of the screen, indicating that you are now in Insert mode.
Type in the desired text that you want to insert into the file. You can navigate within the Insert mode using the arrow keys or other navigation keys.
Once you're done inserting text, press the Esc
key to switch back to the Normal mode. Now you can navigate and perform various editing operations again.
Saving and Closing Files in Vim
To save your changes and exit Vim, follow these steps:
Make sure you are in the Normal mode by pressing the
Esc
key.Type
:w
and press Enter. This command tells Vim to save the changes made to the file.To exit Vim, type
:q
and press Enter. This command quits Vim and returns you to the command line.
Congratulations! You have successfully saved your changes and exited Vim.
Opening a File in Vim to Make Further Edits
Let's say you want to make further edits to the file. To open the file again in Vim, use the same command as before:
vim myfile.txt
You will notice that the file opens in the same state as when you left it. Now you can navigate and make additional edits to the text.
Exiting Vim Without Saving Changes
If you find yourself in a situation where you want to exit Vim without saving the changes, follow these steps:
Make sure you are in the Normal mode by pressing the
Esc
key.Type
:q!
and press Enter. The:q
command tells Vim to quit, and the exclamation mark (!
) overrides any unsaved changes.
Remember, using :q!
will discard any changes made since the last save, so be careful when using this command.
Wrapping Up
In this lesson, we explored the very basics of editing files using Vim. We learned how to open a file, switch between the Normal and Insert modes, navigate within the file, and save changes using :w
and exit Vim using :q
.
Now it's your turn to practice! Create a new file using touch, open it in Vim, insert some text, save the changes, and exit Vim. Then open the file again, make some further edits, and this time exit without saving using :q!
.
Vim may take some time to get used to, but with practice, you'll become more comfortable and efficient in editing files using this powerful text editor.
Keep exploring the Linux command line, and stay tuned for the next lesson, where we'll cover more useful commands and techniques!