How do I run an Ex command on exactly the current visual selection without typing the range manually?
Answer
:
Explanation
In Visual mode, typing : does more than open the command line: Vim automatically inserts the exact selection range as '<,'>. That means you can run any Ex command against only the selected lines without manually typing ranges. For experienced users, this is a fast bridge between visual selection and command-line power.
How it works
- Enter Visual or Visual Line mode and select the target text/lines
- Press
: - Vim opens the command line with
:'<,'>prefilled - Append any Ex command (
s,normal,sort,g, etc.) and press<CR>
Because the range is injected automatically, you avoid off-by-one mistakes and keep your command intent focused.
Example
Select a block of lines in Visual Line mode, then press : and run:
s/^\s\+//
Vim executes it as:
:'<,'>s/^\s\+//
Only the selected lines are affected; the rest of the file stays untouched.
Tips
- Works with complex commands too:
:'<,'>normal! A; - Reuse the same range later with
'<,'>even after leaving Visual mode - Especially useful when composing one-off refactors where macro setup would be overkill