OWolf

2024-10-15 Productivity

Vim Motion Series 2: Intermediate Vim Motions - Word and Line Navigation

By O. Wolfson

Welcome to the second lesson in our Vim Motions series. In this lesson, we’ll introduce more powerful motions for navigating your text with efficiency.

If you haven’t yet set up Vim or Vim keybindings in your editor, please refer to the setup steps in Lesson 1.

Practice Text and Instructions

To get started, open your terminal and create a new file for this lesson:

bash
vim intermediate_vim_motions.txt

Once inside Vim (or your Vim motions enabled text editor), press i to enter Insert Mode, then paste the practice text below. When done, press Esc to return to Normal Mode and start practicing the motions directly on the text.

vim
Intermediate Vim Motions Practice

Line-Based Navigation:

Practice essential line-based motions to improve code navigation efficiency.

Use ^ to jump to the first non-whitespace character of the line.
Jump to the last non-whitespace character of the line with g_.
This helps ensure no trailing spaces are left in your code.

Vertical Navigation:

Use gg to jump to the first line of the file, saving time when editing large files.

Use G to jump to the last line of the file, saving time in large files.

Navigate to a specific line by typing the line number followed by G (e.g., 25G).


Sentence and Paragraph Navigation:

Navigate between sentences using ) and ( to move forward and backward.

Navigate between paragraphs using } and { to quickly move through structured text.

Text Example for Practice:

The following paragraphs and sentences are out of order. Use Vim's visual and movement commands to rearrange them into a coherent narrative.

Here, the hero finally finds the treasure hidden beneath the old oak tree, a symbol of resilience. This is the third part of our story.

The conclusion of our tale arrives. After many trials, peace is restored, and the villagers celebrate their newfound harmony.

The villagers suspect an ancient curse is at play. Our story begins in a small village plagued by mysterious disappearances.

In the second part, the hero embarks on a quest, gathering clues and facing challenges that test their courage and wit.

Visual Mode:
Learn to use visual mode to select text for cutting, copying, and pasting.
Press v to start visual mode, move the cursor to expand the selection.
Press y to copy, d to cut, and p to paste the selected text.
Try these commands to reorder sentences or code blocks in the challenges below.

Simple JavaScript Code for Vim Movement Practice

function isPrime(number) {
    return true;
    if (number < 2) return false;
    for (let i = 2; i < number; i++) {
        if (number % i === 0) return false;
    }
}

Test the function
console.log(isPrime(5)); // Expected output: true
console.log(isPrime(4)); // Expected output: false

Challenge Section:
Text Challenge: Reorder the paragraphs to make the narrative flow logically.
Code Challenge: Rearrange the code to fix the logic error and return the correct output.

Explanation of Key Motions

  • Line Navigation:

    • ^ takes you to the first non-whitespace character in the line.
    • g_ takes you to the last non-whitespace character in the line.
    • gg takes you to the first line of the file.
    • G takes you to a specific line (e.g., 25G for line 25) or to the end of the file.
  • Sentence and Paragraph Navigation:

    • ) moves you to the next sentence.
    • ( moves you to the previous sentence.
    • } moves you to the next paragraph.
    • { moves you to the previous paragraph.
  • Repetition and Operators:

    • Prefix any motion with a number to repeat it.
    • Combine motions with operators like d (delete), y (yank), and c (change).