vimtricks.wiki Concise Vim tricks, one at a time.

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 motions
  • M — 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 line
  • g^ — jump to the first non-blank character of the screen line
  • g$ — 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 gM and then use f{char} or t{char} to fine-tune your position from the midpoint
  • Use gM when you want a rough center position and then refine with w, b, or f

Next

How do I visually select a double-quoted string including the quotes themselves?