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

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// for I# in shell/python files.
  • Use A // instead of I// if you want suffix comments.
  • Keep normal! (with !) when mappings might interfere.

Next

How do I join a wrapped paragraph into one line without manual cursor moves?