How do I prepend // to every line using one Vim command?
Answer
:g/^/normal I// <CR>
Explanation
When you need to comment a whole block quickly, :global combined with :normal is faster than recording a macro or entering Visual Block mode. This pattern executes a Normal-mode edit on every matching line, so it scales well when your file has scattered sections that still follow a clear match rule. It is especially useful for temporary instrumentation and fast code reviews where you want to disable code without deleting it.
How it works
:g/^/normal I// <CR>
:g/^/matches every line (^is start-of-line, present on all non-empty lines and also empty lines)normalruns a Normal-mode command for each matched lineI//inserts//at the first non-blank character of each line<CR>executes the Ex command when typed interactively
Example
Before:
call_api()
render_view()
After:
// call_api()
// render_view()
Tips
- Use a narrower match to target only certain lines, for example
:g/TODO/normal I// - Replace
I//with any repetitive prefix operation, such asI#for shell scripts