Tag Archives: vim

replace a block of text in Vim

Select the block of text: 

1. Position the cursor at the beginning of the text you want to replace

2. Press v to enter visual mode. (or upper case V to select whole lines, or Ctrl-v for a vertical block). 

3. Move the cursor to the end of the text to be replaced. (While selecting text, you can perform searches and other advanced movement, a feature that sets vim apart from most other editors.)

4. Press c. This deletes the selection and leaves you in insert mode.

5. Type your replacement text.

Selection, Cut, Copy and Paste in Vim

The selection is done in visual mode.

To cut (or copy) and paste using visual selection: 

1. Position the cursor at the beginning of the text you want to cut/copy.

2. Press v to begin character-based visual selection (or upper case V to select whole lines, or Ctrl-v for a vertical block).

3. Move the cursor to the end of the text to be cut/copied. (While selecting text, you can perform searches and other advanced movement, a feature that sets vim apart from most other editors.)

4. Press d (as in “delete”) to cut, or y (as in “yank”, which I imagine meaning “yank so hard and fast that it leaves a copy behind”) to copy. 

5. Move the cursor to the desired paste location. 

6. Press p to paste after the cursor, or P to paste before it. 

 

Bonus Tip: To replace the selected text with new text (to be entered by you), press c instead of d or y on step 4. This deletes the selection and leaves you in insert mode. Then, instead of (or prior to) steps 5-6, type your replacement text. 

What is vim recording?

vim with recording turned on

Recording is a really useful feature of Vim. 

You start recording by pressing “Q” key followed by any other letter keystroke, and you can end it by typing “Q” again. 

It records everything you type. You can then replay it simply by typing “@“ followed by the letter. 

Record search, movement, replacement…

This is one of the best features of Vim.

One more thing to note is that you can hit any number before the “@“ to replay the recording that many times like (100@<letter>) will play your recorded actions 100 times.