vi editor basics: In the classic vi editor (or Vim in vi-compatibility mode), which command saves the current file to disk but keeps you in edit mode (does not quit)?

Difficulty: Easy

Correct Answer: :w

Explanation:


Introduction / Context:
Efficient editing in UNIX/Linux often relies on the vi editor (and its enhanced successor, Vim). Knowing the difference between save-only and save-and-quit commands is essential to prevent accidental exits and to preserve work frequently during long edit sessions.



Given Data / Assumptions:

  • You are editing a file in vi/Vim using ex-style colon commands.
  • You want to write (save) the file to disk but stay in the editor to continue working.
  • No special file permissions issues are involved (writable buffer).


Concept / Approach:

Colon commands in vi provide file-level and session-level actions. The command :w writes the current buffer to the associated filename. Commands like :x and :wq both write and exit, while :q attempts to quit and :q! forces quit without saving. Therefore, the save-only action corresponds to :w.



Step-by-Step Solution:

Enter command-line mode by pressing the colon key.Type w to form :w and press Enter.Observe the status message indicating bytes written and the filename.Continue editing; your session remains open in vi.Use :w filename to write to a new file name if needed (save-as behavior).


Verification / Alternative check:

Use :ls or :buffers in Vim to confirm buffer state, and check timestamps with ls -l from a shell to ensure the write occurred. Reopen the file to confirm your recent changes persist.



Why Other Options Are Wrong:

a: :q attempts to quit; it does not save, and it exits if there are no unsaved changes.

c: q! forces quit without saving—dangerous if you intend to keep edits.

d: :x writes and quits (similar to :wq), not save-only.

e: Not applicable because :w is the correct save-and-stay command.



Common Pitfalls:

Mistyping :wq instead of :w, which will exit unintentionally; forgetting to write with elevated privileges when editing protected files (use :w !sudo tee % in Vim); confusing uppercase Q (ex mode) with :q.



Final Answer:

:w

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion