In the vi editor, what does pressing the % key do during text navigation and code editing?

Difficulty: Easy

Correct Answer: to move the cursor to a matching delimiter (parenthesis, bracket, or brace)

Explanation:

Introduction / Context:Efficient navigation in vi (and Vim) includes quickly jumping between paired delimiters. This is invaluable when editing source code that contains nested parentheses (), brackets [], and braces {}. The percent key provides a direct way to verify structural balance and traverse matching pairs.

Given Data / Assumptions:

  • You are in Normal mode in vi/Vim.
  • The cursor is on or near a delimiter character.
  • Source text contains properly paired delimiters.

Concept / Approach:Pressing '%' moves the cursor to the matching partner of the delimiter under the cursor. This works for (), [], and {} and respects nesting. If the cursor is not on a delimiter, vi searches forward on the line for one before attempting a match. This simple motion helps you inspect block boundaries, function scopes, and array indices rapidly.

Step-by-Step Solution:

Place the cursor on a delimiter (for example, '(' or '{').Press % to jump to its match.Use % repeatedly to toggle between the opening and closing pair.Combine with operators (for example, d% to delete to the match) for structural edits.

Verification / Alternative check:In a function with nested blocks, press % at different levels and confirm that vi selects the correct matching partner each time, reflecting proper nesting logic.

Why Other Options Are Wrong:

  • Upper-left corner or first column: use 'H' or '0' respectively, not '%'.
  • Backward to start of word: use 'b' (or 'B' for WORDs).
  • None of the above: Incorrect because '%' matches delimiters.

Common Pitfalls:Pressing % when the cursor is not on a delimiter (it may not jump as expected) or relying on % when delimiters are unbalanced, which can confuse navigation.

Final Answer:to move the cursor to a matching delimiter (parenthesis, bracket, or brace)

More Questions from Unix

Discussion & Comments

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