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

How do I move the cursor to the other end of a visual selection?

Answer

o (in visual mode)

Explanation

While in visual mode, pressing o swaps the cursor to the opposite end of the selection. This lets you expand or shrink either end of your selection without restarting it.

How it works

  • In characterwise visual mode (v): o moves the cursor between the start and end of the selection
  • In linewise visual mode (V): o moves between the first and last selected line
  • In blockwise visual mode (<C-v>): o moves diagonally to the opposite corner, O moves to the other corner on the same line

Example

You start a visual selection from line 10, select down to line 20:

Line 10 (selection start)
...
Line 20 (cursor here)

Press o — cursor jumps to line 10. Now press k to extend the selection upward to line 9.

Press o again — cursor jumps back to line 20. Press j to extend downward to line 21.

Block mode: O vs o

In <C-v> block mode, a block has 4 corners:

A----B
|    |
C----D
  • o swaps diagonally: A ↔ D, B ↔ C
  • O swaps horizontally: A ↔ B, C ↔ D

Tips

  • This is essential when you selected from the wrong starting point
  • Without o, you'd have to cancel the selection and restart
  • Works seamlessly in all three visual modes
  • Many users forget this feature exists and waste time restarting selections
  • Documented under :help v_o

Next

How do I run the same command across all windows, buffers, or tabs?