Which command must be entered before exiting vi to save the current file as filea.txt?
Correct Answer: D
Explanation
The correct answer is D, :w filea.txt. This command will save the current file as filea.txt before exiting vi. The syntax of the command is:
w [filename]
The colon (:) begins command-input mode, which allows the user to enter commands at the bottom of the screen. The w stands for write, which means to save the file. The filename is the name of the file to be saved.
If no filename is given, the command will save the file with the same name as the original file.
Therefore, the command :w filea.txt will save the current file as filea.txt and return to command mode. To exit vi, the user can then enter :q, which stands for quit.
The other commands are incorrect for the following reasons:
* A, %s filea.txt: This command will not save the file, but it will try to substitute the string filea.txt in the file. The syntax of the command is:
%s/pattern/replacement/
The % symbol means to apply the command to the whole file. The s stands for substitute, which means to replace a pattern with another pattern. The pattern is the string to be matched, and the replacement is the string to replace the matched string. However, this command is incomplete, because it does not have a replacement or a delimiter. The command will cause an error and will not save the file.
* B, %w filea.txt: This command will not save the file, but it will try to write the file to a filter. The syntax of the command is:
%w !command
The % symbol means to apply the command to the whole file. The w stands for write, which means to save the file. The ! symbol means to execute an external command. The command is the name of the filter to which the file is written. However, this command is incomplete, because it does not have a ! symbol or a filter name. The command will cause an error and will not save the file.
* C, :save filea.txt: This command will not save the file, but it will try to save the file under a new name and switch to the new file. The syntax of the command is:
saveas filename
The saveas command is similar to the write command, but it also changes the current file name to the new file name. However, this command is incorrect, because it does not have the as keyword after save. The command will cause an error and will not save the file.
* E, :s filea.txt: This command will not save the file, but it will try to substitute the string filea.txt in the current line. The syntax of the command is:
s/pattern/replacement/
The s stands for substitute, which means to replace a pattern with another pattern. The pattern is the string to be matched, and the replacement is the string to replace the matched string. However, this command is incomplete, because it does not have a replacement or a delimiter. The command will cause an error and will not save the file.
References:
* How to Save a File in Vi / Vim Editor & Quit - phoenixNAP
* How to Save a File in Vim / Vi and Quit the Editor | Linuxize
* How to Save a File in Linux After Using the Vi Command - Computer Hope
* How to Save a File in Vi / Vim Editor in Linux - Tecmint
* How to Save a File in Vi / Vim Editor & Quit - GeeksforGeeks