Answer
:'<,'>normal! I//
Explanation
Visual Block insert (<C-v>I...) is great when columns line up, but it can break down on ragged text or mixed indentation. A more robust approach is to select lines in Visual mode and run a linewise Normal command over that range. This lets you prepend text consistently to every selected line while still using Normal-mode precision.
How it works
:'<,'>normal! I// '<,'>is the range of the last Visual selection.normal!executes Normal-mode keys literally (ignoring mappings).I//inserts//at the first non-blank character of each line.
Because I targets the first non-blank character, this keeps indentation intact while adding a consistent prefix. It is especially useful for temporarily commenting blocks, adding logging prefixes, or inserting markers in nested code where blockwise insertion would drift.
Example
Given a Visual line selection:
if ready {
run()
}
After running the command:
// if ready {
// run()
// }
Tips
- Swap
I//forI#in shell/python files. - Use
A //instead ofI//if you want suffix comments. - Keep
normal!(with!) when mappings might interfere.
category: visual-mode tags: #visual-mode #ex-commands #normal-mode #editing
Next
How do I join a wrapped paragraph into one line without manual cursor moves?
Related Tricks
How do I uppercase text inside an HTML tag without changing the tags?
vitU
How do I prepend text to every line in a visual block selection?
I{text}<Esc>
How do I apply my last normal-mode change to every line in a visual selection?
:'<,'>normal! .
How do I remove trailing spaces only within the currently selected visual block?
:'<,'>s/\%V\s\+$//
How do I create a Visual selection for the previous search match instead of the next one?
gN
