Creating and editing text files is one of the most basic tasks that you can perform on a Linux system. The Vi text editor provides you with powerful controls associated with editing text files. But what’s the benefit of spending hours typing when you don’t even know how to save the file?
In this post, we will discuss everything related to editing files in Vi, along with some detailed guides on how to save and quit text files as well.
Vi Editing Modes
There are two editing modes that Vi provides to its users. These are:
- Normal Mode
- Insert Mode
When you open a text file in Vi, the default editing mode is the Normal mode. You can navigate through the file and use some basic Vi commands in this mode. Since the Normal mode does not allow you to edit your files, you will have to enter the Insert Mode to do it.
Simply press the I button on your keyboard to activate the Insert Mode. This mode allows you to modify the content of a text file easily by adding and deleting characters. To go back to the Normal mode, just hit the Esc key on the keyboard.
Opening a File in Vi
The syntax of Vi commands is quite easy to memorize. The default syntax to create a new text file is:
vi
To create a new text file named textfile.txt, type the following command in your terminal.
vi textfile.txt
Note that if a file with the name textfile.txt is already present on your system, then Vi will open that file instead of creating a new one.
Alternatively, you can launch the Vi editor in your terminal and then type :e textfile.txt to open a new file.
Saving a File in VI
The default command to save a text file in Vi is :w. Keep in mind that you can’t use Vi commands while you’re in the Insert Mode. Therefore, you will have to switch to Normal mode by pressing the Esc key.
To save a file in Vi, enter into Normal mode by hitting Esc on your keyboard. Then, type :w and press Enter to save the file.
You can also save the file with a different name. Just pass the new file name with the :w command.
:w newtextfile
Save and Exit the Vi Editor
As mentioned above, you can save a file in Vi with the :w command. Meanwhile, typing the :q command will quit the editor. You can chain these commands together in order to perform the save and quit actions simultaneously.
Press Esc to enter Normal Mode. Type in :wq and hit Enter to save and quit a text file in Vi. You can use the same commands to save and quit a file in Vim as well.
An alternate way to save and exit in Vi is by using the :x command. While both the :x and :wq commands perform a similar task, they are not alike at all. The primary difference between these two is that the :x command writes the buffer to the text file only when unsaved changes are present.
On the other hand, the :wq command writes the buffer to the file irrespective of any unsaved changes. The :wq command also updates the modification time of the file.
Quit Vi Without Saving
To quit a text file without saving in Vi, press Esc on your keyboard to enter Normal mode. Then, simply type :q! and hit Enter.
Learning the Basics of Vi Editor
Getting to know the basics of a text editor in Linux is important as in many situations, you’ll need to work with system text files. Although there are many text editors available such as Nano, Emacs, and Gedit, Vi and Vim are still considered to be the most reliable by many users.
When you first start with a terminal-based editor like Vi or Vim, memorizing the different commands and syntax in Vim can take a while. The most appropriate approach to this problem is to choose an editor that is well-suited for your needs.