Quit VI Editor Without Saving Changes in Linux
In this article, we will cover how to quit the vi editor without saving your changes in Linux. First, we look at what Linux and VI editor and why we use them, what are its features and how can we use them.
Linux
Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, Cent OS, Red Hat, Debian, and Kali Linux.
VI Editor
Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi editor was released which is known as VI IMproved (VIM).
Modes in Vi Editor
There are two modes in Vi Editor:
- Insert Mode
- Command Mode
Quit the Vi Editor without saving changes
To to quit the vi editor without saving changes follow the below steps:
- If you are in insert mode then press Esc to enter in command mode
- Press “:” colon
- To quit without saving write q!
- Press Enter to exit
There are other options for save and quit which are follows:
Commands |
Description |
---|---|
q |
Quit |
q! |
Quit without saving changes i.e. discard changes. |
r [filename] |
Read data from file called [file_name] |
wq |
Write and quit (save and exit). |
w |
Write to file called [file_name] (save as). |
w! |
Overwrite to file called [file_name] (save as forcefully). |
Example:
Basic Vi Editor Guide
1. Create/Edit a file using Vi editor
vi filename
2. Insert Mode
To make any changes in the file first you need to enter in the insert mode to modify the file. To get into insert mode press button ‘i’ to enter in insert mode.
3. Command Mode
To run any command in the vi editor you have to first enter in the command mode if you are currently in the insert mode then press Esc and then ‘:’ colon followed by your command to run your command in the editor.
4. Basic Navigation
To navigate easily within the file vi editor provides a large number of commands which are as follows:
Command |
Description |
---|---|
k |
Moves the cursor up one line. |
j |
Moves the cursor down one line. |
h |
Moves the cursor to the left one-character position. |
l |
Moves the cursor to the right one-character position. |
0 |
Positions cursor at beginning of line. |
$ |
Positions cursor at end of line. |
W |
Positions cursor to the next word. |
B |
Positions cursor to previous work. |
( |
Positions cursor to beginning of current sentence. |
) |
Positions cursor to beginning of next sentence. |
H |
Move to top of screen. |
nH |
Moves to nth line from the top of the screen. |
M |
Move to middle of screen. |
L |
Move to bottom of screen. |
nL |
Moves to nth line from the bottom of the screen. |
Frequently Asked Questions (FAQs)
What is vi editor ?
VI Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility.
I’m not able to write in the editor ?
To modify the text you have to enter in the insert/editing mode first, Press i or a to enter in the insert/editing mode.
What is command mode ?
To run any command in the vi editor you have to first enter in the command mode. To enter in the command mode press Esc and then ‘:’ colon followed by your command to run your command in the editor.
How to save changes in the file ?
To save changes enter in command mode and write ‘:w’ command to save the changes
How to quit the editor forcefully?
If you have any unsaved changes then you can’t quit the editor normally first, you have to enter in the command mode and write ‘:!q’ command to forcefully quit the editor.
Conclusion
In this article, we covered how to quit the vi editor without saving changes in vi editor along with that we covered how to save a file using vi editor, how to quit and forcefully quit the editor and theri associated commands with example. Then we have covered a basic vi editor guide along with the navigation keys and some of the frequently asked questions(FAQs).