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

How do I manually set the visual selection marks to define a precise Ex command range without entering visual mode?

Answer

m<

Explanation

Vim's '< and '> marks record the start and end of the last visual selection and power the '<,'> range used by Ex commands. Most users know these marks are set automatically when exiting Visual mode — but you can also set them by hand with m< and m>, letting you define an arbitrary range for Ex commands without ever entering Visual mode.

How it works

  • m< — sets the '< mark (start of visual range) at the current cursor position
  • m> — sets the '> mark (end of visual range) at the current cursor position
  • After setting both, any Ex command using '<,'> operates on the lines between those marks

Example

Suppose you want to sort lines 5–15 of a long file without scrolling to select them visually:

:5         " jump to line 5
m<         " set start mark
:15        " jump to line 15
m>         " set end mark
:'<,'>sort " sort the marked range

The range '<,'> now covers exactly lines 5–15 — no visual selection needed.

Tips

  • You can verify current mark positions with :marks — look for the < and > entries
  • This technique pairs well with :'<,'>norm! to run Normal mode commands on a precise line range
  • After setting the marks manually, the next time you press : from Normal mode you still get a plain : prompt — type '<,'> yourself or use \:'<,'> in a mapping

Next

How do I make Vim automatically reformat paragraphs as I type so lines stay within the textwidth?