What command is used with vi editor to save file and remain in the editing mode

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Vi-Ex Mode – 1”.

1. To save the editing performed on a file, we have to switch to ______ mode a) command b) input c) ex d) insert

View Answer

Answer: c
Explanation: ex-mode allows us a feature in which we can save the editing that we have performed on a file. ex-mode also provides us features like quitting vi editor, substitution etc.

2. Pressing ‘:’ in command mode invokes which mode of vi editor? a) ex-mode b) insert mode c) command mode d) input mode

View Answer

Answer: a
Explanation: To invoke ex-mode we’ve to press the ‘:’ key in command mode. After pressing : , we are in ex-mode. Now simply type any command of ex-mode to perform a suitable function.

3. To save a file and return to shell prompt, which one of the following command will be used in ex-mode? a) :w b) ‘:x’ c) :a d) :f

View Answer

Answer: b
Explanation: Pressing ‘:x’ in ex-mode will save the file on the disk and returns the shell prompt.

4. To save a file and remain in vi editor, which one of the following command will be used? a) :w b) ‘:x’ c) :q d) :a

View Answer

Answer: a
Explanation: For saving a file and to remain in editor we can use ‘:w’ command in ex-mode. It is generally considered a good practice to save the contents of buffer regularly while working with files using ‘:w’.

5. After pressing ‘:w’, we can normally resume our editing work. a) True b) False

View Answer

Answer: a
Explanation: Pressing ‘:w’ doesn’t quit the editor but simply saves the editing we’ve done so far in the specified file.

Check this: Unix Books | MCA Books

6. We can specify the filename with ‘:w’ command. a) True b) False

View Answer

Answer: a
Explanation: To save our editing to a different file, we can specify the name of the file in which we want to save the work. To do this, simply provide the filename as an argument with ‘:w’ command. For example, to save the contents to file named ‘file002’, use the following command:

7. Which command is used for saving and quitting the editor simultaneously? a) :q b) :w c) ‘:x’ d) :f

View Answer

Answer: c
Explanation: ‘:x’ command saves our content and quits the editor simultaneously. This command is invoked in ex-mode.

8. Which of the following command is a substitute for ‘:x’ command? a) ‘:x’ b) :wq c) :q d) :w

View Answer

Answer: b
Explanation: We can also use the ‘:wq’ command to save and quit editor but it requires an additional keystroke and it is not recommended for use.

9. Which one of the following is the best way of saving and quitting the editor? a) ZZ b) :wq c) :w d) ‘:x’

View Answer

Answer: a
Explanation: The best and preferred way to save and quit the editor is to use ZZ, a command mode command which is way better then ‘:x’ or :wq.

10. Which command is used to abort the editing and quit the editing mode? a) :w b) :wq c) :q d) ‘:x’

View Answer

Answer: c
Explanation: It is possible to abort the editing process and quit the editing mode without saving the buffer. The quit (:q) command performs this job.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

  • Get Free Certificate of Merit in Unix
  • Participate in Unix Certification Contest
  • Become a Top Ranker in Unix
  • Take Unix Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

What command is used with vi editor to save file and remain in the editing mode

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Introduction

Vim (Vi IMproved) is an open-source text editor for Unix and Linux systems. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI.

Take the first step in learning Vim with the basic save and exit commands. Follow this guide and learn how to save a file in Vi/Vim and exit the editor.

What command is used with vi editor to save file and remain in the editing mode

Prerequisites

  • A system running Linux.
  • A user with sudo privileges.
  • Access to a terminal/command line.
  • Vim installed (alternatively, use the default Vi).

When Vim launches, files are opened in command mode by default. This means that you can move around and edit the file, but cannot insert new text.

All alphanumeric keys in this mode are equal to commands, and pressing them will not display their value on the screen. For example, pressing the letter w will move the cursor one word forward.

To type text, you must be in insert mode. To switch to insert mode, press the i key. Now you can type text into a file.

To switch back to command mode, press the Esc button.

Vim offers different commands to save changes to a file, depending on whether you need to exit the text editor or not. Below are examples demonstrating how to save and exit, save without exiting, and exit without saving.

Start by opening a new text file using Vim:

vim example_file.txt

Next, press i to enter insert mode and add some text.

To save a file and exit Vim, do the following:

1. Switch to command mode by pressing the Esc key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type x after the colon and hit Enter. This will save the changes and exit.

What command is used with vi editor to save file and remain in the editing mode

Alternatively, a command that will also save a file and exit the text editor is:

:wq

In which case the w stands for write (save) and q stands for quit.

To save a file without exiting in Vim:

1. Switch to command mode by pressing the Esc key.

2. Type : (colon). This will open the prompt bar in the bottom left corner of the window.

What command is used with vi editor to save file and remain in the editing mode

3. Type w after the colon and hit Enter. This will save in Vim the changes made to the file, without exiting.

You can also rename an existing file by adding the new name after the commands.

:w [newfilename]

In the picture below, we renamed the existing example1.txt into newexample.

What command is used with vi editor to save file and remain in the editing mode

To exit Vim without saving changes:

1. Switch to command mode by pressing the Esc key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type q! after the colon and hit Enter to exit without saving the changes.

What command is used with vi editor to save file and remain in the editing mode

The q! quits the text editor.

Conclusion

This article explained how to save files in Vi / Vim, with multiple command options.

The more comfortable you feel using keyboard shortcuts, the more you will understand why Vim earned its worldwide reputation. Next learn how to copy and paste in Vim, and how to go to the end of the file!