vi/Vim navigation shortcut: In the vi editor, typing a line number followed by the uppercase letter G moves the cursor where?

Difficulty: Easy

Correct Answer: to the beginning of the line specified by the number

Explanation:


Introduction / Context:
Efficient navigation is the hallmark of expert vi/Vim usage. Jumping directly to a specified line saves time when editing large files or investigating error messages that include line numbers. The [number]G motion is one of the fastest ways to reposition the cursor precisely.



Given Data / Assumptions:

  • We are in command (normal) mode in vi or Vim.
  • A numeric prefix is entered before the uppercase letter G.
  • We want to know the effect of this motion on cursor position.


Concept / Approach:

In normal mode, G without a count moves to the end of the file. Prepending a count moves to that exact line number. The cursor is placed at the first non-blank character of the target line (behavior may vary slightly with settings), effectively moving to the beginning for editing purposes.



Step-by-Step Solution:

Enter normal mode (press Esc).Type the desired line number, for example 120.Press uppercase G: the cursor jumps to line 120.Verify with :. (print current line number) or :set number to see line numbers.


Verification / Alternative check:

Use :120 as an alternative command-line mode jump, which also moves to line 120. Both methods place the cursor on the specified line, confirming the mapping of [line number] + G.



Why Other Options Are Wrong:

  • Left/right by one position: That is h and l in normal mode.
  • Down by one line: That is j; G with a count is a direct jump, not a single-line move.
  • None of the above: Incorrect because [number]G is exactly the go-to-line operation.


Common Pitfalls:

Using lowercase g (a different motion in Vim), forgetting to be in normal mode, or typing an invalid line number, which causes movement to the nearest valid boundary (start or end). For extremely large files, consider :set number and search commands like /pattern to combine jumps and searches.



Final Answer:

to the beginning of the line specified by the number

Discussion & Comments

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