brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Ctrl-v and I in Vim type the same text on many lines at once

Vim
~/tips/tip-vim-block-insert

Ctrl-v

Adds a prefix to a column of lines in one edit.

Commenting out ten lines, or putting a dash in front of each item, is a job for Vim’s block selection.

Ctrl-v
9j
I# <Esc>

Ctrl-v starts a visual block. Move down with j or a count to cover the lines you need. I starts inserting before the block, and what you type appears on the first line only. The moment you press Escape, Vim copies it to every line of the block.

A does the same after the block. Combine it with $ to append to the end of lines of different length:

Ctrl-v
5j$
A;<Esc>

I’d remove a prefix the same way: select the column with Ctrl-v and press d.

On Windows terminals and in some setups Ctrl-v pastes instead. Vim also accepts Ctrl-q to start a visual block there.

Short lines are the thing to watch. With I, a line that is shorter than the block’s left edge doesn’t get the text. Start the block in column one, with 0, and you won’t run into that.

Further reading: Vim help: visual block insert, Vim help: visual block append

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link