OWolf

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 everything 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

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)
Move down 5 lines and up 3 lines to navigate through this text:
5j
3k
2w   Jump forward two words to 'consectetur'
w    Jump to the next word
e    Jump to the end of 'adipiscing'
b    Move back one word to 'elit'
2b   Move back two words to 'Lorem'

Part 2: Line-Based Navigation (Jump to the start/end of lines)
^    Jump to the first non-whitespace character of this line
g_   Jump to the last non-whitespace character of this line
0    Jump to the very start of the line
$    Jump to the very end 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
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vehicula nunc quis dui.
(    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

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) into register 'b', press v to enter visual mode, 3j to select the next 3 lines, : to enter command mode, and "by to yank the selected lines into register 'b':

Paste the lines from register 'b':
"bp

Now delete the word 'vehicula' without affecting your clipboard:
"_diW

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:
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

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:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vehicula nunc quis dui egestas. Suspendisse et facilisis odio. Integer sodales vehicula sem. Sed ac lacinia nunc. Nam posuere interdum nulla.