How do I jump to the middle of a line in Vim?
Answer
gM
Explanation
The gM command moves the cursor to the horizontal middle of the current line, regardless of how long the line is. This is particularly useful when editing long lines and you want to quickly navigate to the center without counting characters.
How it works
g— prefix that enables extra motionsM— moves to the middle column of the visible line
Unlike 50| (which jumps to column 50), gM adapts to the actual length of the line, always landing at its midpoint.
Related commands:
g0— jump to the first character of the screen lineg^— jump to the first non-blank character of the screen lineg$— jump to the end of the screen line
Example
Given a line of 80 characters:
foo bar baz qux quux corge grault garply waldo fred plugh thud wibble wobble
Running gM places the cursor approximately at the middle of that line — around the word grault.
Tips
- Works on wrapped lines — it targets the middle of the screen line, not the file line
- Combine with
gMand then usef{char}ort{char}to fine-tune your position from the midpoint - Use
gMwhen you want a rough center position and then refine withw,b, orf