5.1 Save and Exit


It is necessary to know the method of starting and exiting as the most basic operation of vi command (vim) that is the editor that edits a file.

The method of starting the editor is to execute the vi command, the method of exiting the editor is to input an exit command.

There are several commands for exiting the editor as follows.


 Command  Action
 :q↵  Quit
 :q!↵  Quit by force
 :w↵  Write
 :w!↵  Write by force
 :wq↵  Write and Quit
 :wq!↵  Write and Quit by force

Table 5-1: Write & Quit Command

5.1.1 Open a file and exit

Format

vi [file-name]


To open a file with vi command, you need to specify a file name which you want to edit after vi command.


Practice: Open and close a file


Specify the existing file, "/etc/hosts". Here, you only start and exit from vi command without editing a file.


$ vi /etc/hosts ↵

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1         localhost.localdomain localhost
"/etc/hosts" 7L, 147C


If you have not changed anything in the file and want to quit, use ":q" command.


:q↵
$



5.1.2 Quit without saving

If you don't want to save a file even though you edited a file, you can quit by force.


Practice: Close a file without saving after editing


Specify the non-existing file, "viquit.txt" because you want to exit after editing.


$ vi viquit.txt ↵
"viquit.txt" [New File]


Input something. Type " bbbbbcccccddddd [Esc]". It will be explained in detail about input command and the meaning of [Esc] key later. Therefore now, let's type as it shows.

If the file was edited, it is impossible to exit without saving using ":q" command, the error message is shown as follows.


bbbbbcccccddddd
:q↵
E37: No write since last change (add ! to override)


If you want to exit without saving even though you edited a file, quit by force using ":q!" command.


:q!↵
$


If you want to continue editing after an error display, please input a command or push ENTER.

5.1.3 Saving a file

After editing something in a file, If you want to keep the change in the file and continue editing you can save edited file.


Practice: Save a file after editing


After editing a file, save a file.


$ vi viwrite.txt ↵
"viwrite.txt" [New File]       0,0-1       All



Input something. Type " abbbbbcccccddddd [Esc]". It is explained in detail about input command and the meaning of [Esc] key. Therefore now, let's type as it shows.

Save a file, then continue editing. If you don't want to exit after saving a file, you should use ":w" command. When the file is saved, it shows some information such as the file name, number of line and character, operation, current position.


abbbbbcccccddddd
:w↵
"viwrite.txt" [New File] 1 line, 16 characters written


Sometimes, an error happens in saving a file.

In case of that, if it is a file which you created, you have a privilege to rewrite. Therefore, you can write a file using ":w!" command, then continue to edit.


bbbbbcccccddddd
:w!↵
"viwrite.txt" 1 line, 16 characters written


After saving a file, you can exit by ":q" command.


:q↵
$


 Command  Action
 :q↵  Quit
 :q!↵  Quit by force
 :w↵  Write
 :w!↵  Write by force
 :wq↵  Write and Quit
 :wq!↵  Write and Quit by force

Table 5-2: Write & Quite command


5.1.4 Exit with saving a file

After editing, you may want to save and exit at once.

Practice: Exit with saving a file


Specify non-existing file "viwritequit.txt".

$ vi viwritequit.txt↵


"viwritequit.txt" [New File]


The command for saving a file is ":w", and the command for exiting is ":q".

If you want to save a file and exit, you can write two command continuously like a ":wq".


aaabbbccc
:wq ↵
"viwritequit.txt" [New File] 1 line, 10 characters written
$


If you want to save and exit by force, you can use ":wq!" command.


:wq ↵
"viwritequit.txt" [New File] 0 line, 0 characters written
$


 Command  Action
 i  Insert before cursor
 a  append after cursor
 I  Insert at the beginning of line
 A  append after the end of line

Table 5-3:Commands for moving into insert mode



Previous Next