VOOZH about

URL: https://vimtricks.wiki/posts/prepend-selected-lines-with-normal-command

⇱ How do I prepend text to every selected line even with uneven indentation? | VimTricks


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.

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