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

How do I move to the first or last non-blank character of a soft-wrapped display line?

Answer

g^ and g$

Explanation

When wrap is on, long lines wrap visually across multiple screen lines. Standard ^ and $ always operate on the logical line (the whole line including its wrapped portions). The g^ and g$ variants operate on the display line — the segment of text currently visible on that screen row.

How it works

  • g^ — move to the first non-blank character of the current display (screen) line
  • g$ — move to the last character of the current display line
  • g0 — move to the very first character of the display line (like 0 for display lines)
  • Compare to gj/gk, which move up/down by screen lines rather than file lines

Example

With set wrap enabled and a long paragraph wrapping to 3 screen lines:

    This is the first screen line of a long paragraph that
wraps onto the second screen line here, and the third
screen line ends here.

With the cursor on the second screen line, g^ jumps to w in wraps, and g$ jumps to e in here,.

With ^ or $, the cursor would jump to the logical line start/end (the T of This or the final .).

Tips

  • gm — move to the middle of the display line
  • These commands are most useful when writing prose in Markdown or plain text with set wrap
  • In combination: g^ then vg$ visually selects the current display line exactly
  • With set nowrap, display lines equal logical lines, so g^/g$ behave identically to ^/$

Next

How do I enable matchit so % jumps between if/else/end style pairs?