How do I insert the same text at the start (or end) of multiple lines simultaneously?
Answer
<C-v>{motion}I{text}<Esc>
Explanation
Visual block mode (<C-v>) lets you select a rectangular region across multiple lines. Using I (insert before the block) or A (append after the block) while in visual block mode inserts text on every selected line simultaneously — making it the fastest way to add prefixes like comment markers or list bullets to a range of lines.
How it works
<C-v>— enter visual block mode{motion}— extend the selection downward (e.g.,3jfor 4 lines,Gto end of file)I— enter insert mode before the left edge of the block on all selected lines{text}— type the text you want inserted (you only see it on one line while typing)<Esc>— the text is replicated to every line in the selection
Use A instead of I to append after the right edge of the block on each line.
Example
To comment out three lines of JavaScript by prepending // :
const a = 1
const b = 2
const c = 3
- Place cursor on
const a = 1 <C-v>2j— select all three lines as a blockI// <Esc>— insert//before each line
Result:
// const a = 1
// const b = 2
// const c = 3
Tips
- On lines of different lengths,
Aappends after the longest line's column on shorter lines — use$beforeAto append after each line's own end:<C-v>{motion}$A{text}<Esc> - To delete a visual block (e.g., remove a leading
//), select the two-character block with<C-v>and pressd - In Neovim, changes appear on all lines in real time; in Vim, you see them only after
<Esc>