In the vi editor, which single-key command replaces exactly one character under the cursor with new text you type?

Difficulty: Easy

Correct Answer: r

Explanation:


Introduction / Context:
Vim/vi provides powerful one-keystroke editing commands for rapid text manipulation. Knowing which key performs a single-character replacement saves time and reduces errors, especially when editing configuration files or code directly on servers.


Given Data / Assumptions:

  • You are using vi or a compatible Vim in normal mode.
  • You want to replace exactly one character at the cursor position.
  • You will type the replacement character immediately after invoking the command.


Concept / Approach:

The r command in vi replaces the single character under the cursor with the next character you type. It does not enter insert mode; instead, it performs an in-place substitution and returns to normal mode instantly. This differs from commands like s (substitute one character and enter insert mode), S (change the entire line), and C (change from cursor to end of line).


Step-by-Step Solution:

Navigate the cursor to the character to change.Press r.Type the single replacement character (e.g., X).vi performs the replacement and remains in normal mode.


Verification / Alternative check:

Compare with s: pressing s deletes the character and switches to insert mode, allowing multiple characters to be typed before pressing ESC. For a strict one-character swap without entering insert mode, r is correct.


Why Other Options Are Wrong:

  • S: changes (replaces) the entire line and enters insert mode.
  • s: substitutes one character but enters insert mode for additional input.
  • C: changes from the cursor position to end of line.
  • None of the above: incorrect because r is precisely the single-character replace command.


Common Pitfalls:

  • Forgetting mode: r works in normal mode only.
  • Confusing r with s; s transitions to insert mode, which is different behavior.


Final Answer:

r.

Discussion & Comments

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