October 6, 2024
O. Wolfson
In this part of the Vim Motion series, we’ll dive into essential motions and commands. These motions will help you manipulate text efficiently and navigate your files like a pro. We’ll cover everything from re-selecting text to visual block mode and advanced text replacement techniques.
After performing a change, you can repeat it using the .
command:
.
: Repeats the last command. This is particularly useful when you want to apply the same modification across multiple lines or sections.For example, if you delete a word with dw
, you can repeat the same deletion on the next word by simply pressing .
.
Vim allows you to quickly undo and redo your changes:
u
: Undo the last change.Ctrl-r
: Redo the last undone change.These motions are critical when editing text, allowing you to easily backtrack or redo operations without losing your progress.
When you need to modify columns of text or apply changes across multiple lines, visual block mode comes in handy:
Ctrl-v
: Enter visual block mode.y
), delete (d
), or change (c
) to the selected block.For example, if you have several lines of text and need to delete the first few characters in each line, visual block mode allows you to select those characters and delete them all at once.
Sometimes you need to re-select the last block of text you worked on. Vim provides a quick way to do this:
gv
: Re-select the last visual selection.This command is a huge time saver when you need to reapply changes or adjustments to the same selection.
You can delete or change the word under the cursor with one command:
diw
: Delete the word under the cursor.ciw
: Change the word under the cursor (delete and start insert mode).These commands are efficient when you want to quickly modify specific words without entering visual mode.
To adjust the indentation of a block of text, use the following commands:
>
: Indent the selected lines.<
: Un-indent the selected lines.This is particularly useful when formatting code or structured text.
Let’s put these motions into practice. Open a new file for this session:
bashvim motion_practice.txt
Copy this block of text into the file and follow the instructions to practice the motions:
text1. Undo the last change using 'u'. 2. Redo the undone change with 'Ctrl-r'. 3. Select the following lines using visual block mode (Ctrl-v) and delete the first word of each line: - Example text for block deletion. - Another line for practice. - Yet another line of text. 4. Use 'ciw' to change a word within this sentence. 5. Indent and un-indent the lines of text using '>' and '<'.
By mastering these essential Vim motions, you’ll enhance your efficiency when navigating and modifying text. From repeating commands to using visual block mode, these motions streamline your workflow and make editing in Vim even more powerful. Keep practicing these techniques to make them second nature!