How do I prepend text to every selected line even with uneven indentation?
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.