2024-10-15 Productivity

Vim Motion Series 12: Master Vim Motions

By O. Wolfson

Welcome to the final part of the Vim Motion Series. If you've made it through the previous 11 articles, you should now have a solid foundation in Vim's powerful motion and navigation features. This article is a review of what we've covered so far, linking you back to each article, and providing a master practice block that will challenge your skills and help you upgrade your Vim efficiency.

Review of Previous Articles

Here are the links to all the articles in this series:

  1. Introduction to Vim Motions
  2. Intermediate Vim Motions: Word and Line Navigation
  3. More Efficient Horizontal and Vertical Movement
  4. Mastering Search and Find Using Vim Motions
  5. Copy and Paste in Vim
  6. Essential Vim Motions for Efficient Text Editing
  7. Visual Mode and Text Object Selections
  8. Mastering Jumps, Marks, and Efficient Navigation
  9. Combining Motions with Editing Commands for Enhanced Workflow
  10. Advanced Search, Replace, and Regular Expressions
  11. Navigating Multiple Sections and Files

Practice Block

Copy and paste the following text into your Vim editor and practice the motions discussed in each article.

vim
Part 1: Horizontal and Vertical Motions (Start practicing simple movements)
Use j and k to navigate up and down the screen. h and l to navigate left and right.

šŸ–ļø put your cursor on the hand. Use 5j to move down 5 lines
Use w to jump to the next word until you get to the line below.
Use 9w Jump forward nine words to the hand šŸ–ļø.
Use e to jump to the end of words šŸ‘€ until just after the hand šŸ–ļø here.
Use b to move back to the eyes in the above line.
Use 4k to move up 4 lines.

Put your cursor here šŸ–ļø. Use 4b to move back four words to 'Put'

Part 2: Line-Based Navigation (Jump to the start/end of lines)

g_   Jump to the last non-whitespace character of this line
^    Jump to the first non-whitespace character of this line
$    Jump to the very end of the line
0    Jump to the very start of the line
gg   Jump to the first line of this file
G    Jump to the last line of this file
50G  Jump to line 50 (or any number here)
25G  Jump to line 25

Part 3: Sentence and Paragraph Navigation (Use text for sentence jumps)
Navigate through the following text with sentence and paragraph motions:

)    Jump to the start of the next sentence
(    Jump to the previous sentence
}    Jump to the start of the next paragraph
{    Jump to the previous paragraph

H   Jump to the top of the screen.
M   Jump to the middle of the screen.
L   Jump to the bottom of the screen.

zz   Center the screen on the cursor.

Part 4: Advanced Search and Find (Search through the text below)
Search for 'Lorem' in this text:

/Lorem

n     Jump to the next match
N     Jump to the previous match

You can clear the search with `:noh`

Now, search backward for 'facilisis':
?suspendisse

n     Jump to the next match
f,    Find the next occurrence of ',' in the current line
;     Repeat the last find command

Part 5: Copy and Paste (Copy and paste various lines and registers)

Yank this line into register 'a':
"ayy

Paste the line from register 'a':
"ap

Yank multiple lines (3 lines below) into register 'b', move down to the first line, press v to enter visual mode, 3j to select the next 3 lines, use "by to yank the selected lines into register 'b':

1. this is the first text to yank
2. this is the second text to yank
3. this is the third text to yank

Paste the lines from register 'b':
"bp

copy this line with "ayy
copy this line with "byy
copy this line with "cyy

Now paste each of the registers:
"ap
"bp
"cp

Now delete the word 'motions' without affecting your clipboard:
"_diw

To copy text to the system clipboard, so you can paste it into other applications, use "+y and a motion. You should combine that command with other motions like:

"+yy to copy  the whole line to the system clipboard or
"+yw to copy the next word to the system clipboard or
"+yG to copy everything from the cursor position to the bottom of the doc.

Part 6: Efficient Text Editing (Undo/redo and change words)
u     Undo the last change
Ctrl-r  Redo the undone change
ciw   Change the inner word under the cursor (e.g., replace 'Lorem' with 'Vim')
>>    Indent the current line
<<    Un-indent the current line


Part 7: Visual Mode (Select text blocks)
Use visual line mode to select and yank this block of text:
enter visual line mode with `v`
Example block for visual mode:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nulla facilisis odio suspendisse.

Yank the selected block:
y

Delete the selected block:
d

Part 8: Marks and Jumps (Set and jump to marks)
ma    Set a mark 'a' at the current position
'a    Jump back to mark 'a'
mB    Set a global mark 'B' here
'B    Jump back to global mark 'B'
Ctrl-o  Jump back to the previous location
Ctrl-i  Jump forward again
Set a mark at each section heading. Use m1 to mark Part 1, m2 for Part 2, and so on. Use '1 to return to Part 1 and '2 to return to Part 2.

As you read through this paragraph, set a local mark with `ma` at this sentence. Move around and jump back to it by pressing `'a`. Now, scroll down and set a global mark with `mB` here. Explore other areas or even switch files, then jump back to this global mark using `'B`. After making a few jumps, use `Ctrl-o` to retrace your steps and `Ctrl-i` to move forward again. Practice setting marks at interesting points and navigating between them to master quick movement in Vim.

Part 9: Combining Motions and Operators (Delete, change, and repeat)
d2w   Delete the next two words from the cursor
c2b   Change the previous two words
y$    Yank from the cursor to the end of the line

.     Repeat the last change

Part 10: Search and Replace (Replace words in this text block)
:%s/foo/bar/g  " Replace all occurrences of 'foo' with 'bar'
:%s/\(Lorem\|elit\|vehicula\)/replaced_text/g " Replace these words with 'replaced_text'

Text for practice:

In this text block, try replacing all occurrences of the word **foo** with bar using `:%s/foo/bar/g`. Next, challenge yourself to replace multiple words at once. For example, replace Lorem, elit, and vehicula with replaced_text by running `:%s/\(Lorem\|elit\|vehicula\)/replaced_text/g`.

11. Surrounding text

Select a line in visual line mode with `V` and use `S<div>` to change the whole line to a pair of div tags.