Vim Tip of the Day: Coordinates

We saw that common Vim navigations are based on structure, while others are based on search. Still others are based on position. If you think about your “buffer” (i.e. the text you are working with) as an XY coordinate plane, a sea of latitudinal lines and longitudinal columns with characters occupying each position, Vim provides you ways to navigate that plane via what we’ll call position-based or coordinate motions.

  • Go to line — gg G
    • Defaults to first and last, respectively
  • Go to column in current line — |
    • The in-line analogue to G, this defaults to 1st column, but can be used with a count, e.g. 79|.
    • Note this is the pipeline character.
  • Current status/position?1C-g or gC-g
    • The latter gives more details and can be used on selected regions too, i.e. visual mode, which we’ll learn about next.
  • Go left/down/up/right — h j k l (the usual)
  • Go to top / middle / bottom of page — H M L
  • Go down / up “display lines” — gj gk
    • You’ll need these when long lines are wrapped.
  • Go to first / last column in current line — 0 $
  • Go to first / last column in “display line” — g0 g$
    • Again, for wrapped lines.
  • Go to first / last non-blank column — ^ g_

Some Vim users find it useful to enable some form of visible line numbers and also column numbers to take advantage of coordinate motions. For instance, you might like to do something like d47G to delete everything from the current cursor position to line number 47 — which would be convenient (or would suggest itself at all) only if line numbers were readily visible. This is especially useful in large files, where operating on huge blocks of text would ordinarily involve a delicate balancing act of scrolling and selection. Working with line numbers here is easier and faster.²

Structural, search-based, and coordinate motions are the core types of motions in Vim, which brings us to a unifying idea on the road to fluency:

🌱 All motions are nouns.

(Note that the converse isn’t true — as we learned earlier, some nouns are not motions).

1 This isn’t supported in Emacs Evil out of the box since C-g is the universal interrupt in Emacs. You could use this function together with the built-in count-words to mimic the functionality, if you’d like to have it.

² Incidentally, line numbers are also useful for pair programming sessions, providing participants with a way to refer to sections of the visible code precisely.

Leave a Reply

Your email address will not be published. Required fields are marked *