Vim Motions - A Generalists Guide
Vim can be a wild place without a firm understanding of the motions required to navigate the interface. What at first seems painful by design yields many productivity benefits.
If your only experience with Vim is getting stuck in a Vim instance, this article is for you!
Basic Modes of Vim
First, let's familiarize ourselves with the basic modes of Vim. Vim operates in four primary modes, each designed for specific tasks.
- Normal mode: The default state you'll see when entering Vim. Typically used for navigating throughout your document and entering into other vim modes.
- Insert mode: This mode resembles a traditional text editor and is the mode people frequently get stuck in.
- Visual mode: Commonly used for highlighting, selecting blocks of text to yank or delete, and more.
- Command mode: Accessed through Normal mode. Enabled the ability to execute commons over files. Think of the ability to save files, search, replace text, execute shell commands, and more. Command mode must be accessed through Normal mode.
Navigating with Vim Motions
The rest of this article touches on a handful of common Vim motions frequently used within Vim. A firm understanding of these is a healthy starting point in one's Vim journey.
These simple keystrokes allow you to move around and navigate through a file.
jmoves the cursor up.kmoves the cursor down.hmoves the cursor left.lmoves the cursor right.wjumps forward by a word.bjumps backward by a word.
You can compound the action of a single motion by utilizing relative line numbers within your IDE. This allows you to move with Vim motions quickly. i.e., 10j takes you up 10 lines.
Visual mode provides commands that act on selected text.
ddeletes the selected text.dddeletes the current line.ccuts the selected text.yyanks (copies) the selected text.ppastes the yanked or cut text.vstarts visual selection.uundoes the last action.
Visual motions can be mixed with normal motions and relative line numbers.
Heads up! Yanking and deleting lines go to the same buffer, so if you yank a line and later delete a line in visual mode, you'll override the yank with whatever you had deleted.
Most commands related to Insert mode will involve taking you into Insert mode because once you're inside Insert mode - it's a traditional editorial experience.
There are various ways to enter insert mode. Here are two more commonly used approaches to enter insert mode from normal mode.
itakes you to the left of the cursoratakes you to the right of the cursor
Command mode
Let's dive a little deeper into this one; there's a lot you can do here, so consume the list at your own pace or use it as a defense if anything else.
Saving and Exiting
:wsaves the current file but keeps it open for further editing.:qquits the current window. If there are unsaved changes, Vim will warn you.:wqor:xsaves the current file and exits.:q!quits without saving, discarding any changes made since the last save.
Opening and Creating Files
:e filenameopens a file named "filename" in the current window, allowing you to edit a different file without leaving Vim.:tabe filenameopens a file in a new tab, enabling you to work with multiple files.
Searching and Replacing
:/patternsearches for "pattern" in the document. You can navigate through occurrences withn(for next) andN(for previous).:%s/old/new/greplaces all occurrences of "old" with "new" throughout the document. Thegat the end specifies that the replacement should happen globally across the entire file. Withoutg, only the first occurrence in each line would be replaced.
Line Navigation
:numberjumps to the line number specified. For example,:25would take you directly to line 25.:$moves the cursor to the last line of the document.:0or:1takes you to the document's first line.
Setting Preferences (Typically managed in a dotfile but can be done on the fly)
:set numberturns on line numbering. This can help you navigate through your document.:set relativenumberenables relative line numbering, which is particularly useful with Vim's motion commands.:set norelativenumberswitches back to absolute line numbering.:set nowrapdisables text wrapping, which can be useful when editing code or configuration files.
Advanced Motions
Once you're comfortable with the basics, you can explore more advanced motions to enhance your productivity in Vim.
Two common default options for moving lines in Vim are dd and p, and explicitly moving lines. Deleting a line with dd and pasting it with p works, because the deleted line is stored in a buffer.
To explicitly move a single line, you can use the following commands:
:m +1moves the current line down by one.:m -2moves the current line up by two. (Including the current line, this is a net movement of one line up.)
For moving multiple lines, enter Visual mode with V, select the lines you want to move, and then choose your preferred movement command.