Originally, Vim was created to run on terminals without arrow keys or mouse support. The entire editor can be used from the home row of the keyboard with minimal hand movement.
That's why many of its keybindings feel unusual by today's standards.
Learn how Vim keybindings work, internalize them, and you'll be able to move faster in Vim than you would in most conventional editors.
What Is a Vim Keybinding?
Vim keybindings are keyboard shortcuts, a physical key or sequence of keys you press to perform a specific action within the Vim text editor. These keybindings work like in any other system, except that in Vim, they are not optional.
The command-line interface (CLI) version of the editor lacks a toolbar and does not support drag-and-drop features. Even simple actions, such as opening a file, deleting a word, or copying a line are all performed via the keyboard using dedicated bindings.
Note: Vim is not the default text editor in Ubuntu 24.04. Find out how to install the latest Vim version on Ubuntu.
Why Use Vim Keybindings?
Using Vim keybindings has several practical advantages, which include:
- Full keyboard control. Hands always stay on the keyboard, allowing users to issue direct commands for nearly any action instantly. There is no need for repetitive mouse movements or excessive clicking.
- No wasted motion. Keys are positioned to minimize finger movement. Users who internalize Vim keybindings can edit text or code faster and with greater precision than in most editors.
- Reduced strain. Users can perform tasks with less physical movement and strain. This increases focus and reduces fatigue during long editing sessions.
- Consistency. Workflows are more predictable and uniform because the same key-driven interface applies regardless of the task.
- Customization and automation. Users can map frequently used actions to custom keybindings and incorporate complex sequences into scripts to automate repetitive tasks.
Note: Compare Vim with other cross-platform text editors for Linux, Windows, and macOS to more clearly evaluate its benefits.
How to Use Vim Keybindings?
In most editors, pressing a key usually types a character. In Vim, the key's function depends on the keybinding associated with it and the current mode Vim is operating in.
Vim Modes
Switching between different modes is fundamental to how Vim works and is just as important as hitting the correct key.
For example, in Normal mode, pressing d
starts a delete command. In Insert mode, the same key just types the letter d. Users often navigate through a text document in Normal mode, delete a section, switch to Insert mode to type replacement text, and then return to Normal mode to continue navigating.
Vim displays the current mode in the lower-left corner of the terminal window, except for Normal mode, which shows no prompt.
The following table lists the most commonly used Vim modes:
Mode | Description | Shortcut to Enter |
---|---|---|
Normal Mode | The default mode for navigating and selecting text, and running commands. You cannot type text in this mode. | Esc |
Insert Mode | Type and edit text directly, like in any other text editor. Use this mode when adding new content. | i , I , a , A , o , O |
Visual Mode | This mode allows users to select text, similar to clicking and dragging with a mouse, only using the keyboard. | v (character), V (line), Ctrl+v (block) |
Command-Line Mode | Run commands to save changes, exit files, or find and replace text in Vim. | : |
Replace Mode | Overwrite existing text one character at a time. | R |
Select Mode (rarely used) | Similar to Visual mode, but mimics the way text is selected in graphical editors. | Depends on the Vim build. |
Combining Keybindings
Many Vim commands consist of a single key, but more powerful operations combine three elements:
- Operator. Defines what action to perform.
- Count (Optional). A numerical prefix that specifies how many times to apply the motion.
- Motion. Tells Vim the scope and direction in which the action will be performed.
Users can combine operators, counts, and motions to perform complex actions by entering a sequence of keys in rapid succession.
For example, the following keybinding (in Normal mode) deletes the next four words after the cursor:
d4w
Keybinding | Description |
---|---|
d | The delete operator. Tells Vim to remove text based on the motion. |
4 | Repeats the forward motion (w ) 4 times. |
w | Move forward by one word at a time. Repeat the motion four times due to the count value (4). |
The following sections explain how to navigate and edit text using keybindings in different Vim modes.
Navigating Using Keybindings
Normal mode is the default mode for navigating and manipulating text in Vim. Most keybindings in this mode are single-letter commands.
Moving by Character or Line
Use the following keys to move the cursor left, right, up, and down:
Key | Description |
---|---|
h | Move the cursor one character left. |
l | Move the cursor one character right. |
j | Move the cursor one line down. |
k | Move the cursor one line up. |
Moving by Word or Sentence
The following commands enable users to traverse larger sections of text in one go:
Key | Description |
---|---|
w | Move forward to the start of the next word. |
W | Move forward to the start of the next word and ignore punctuation. |
b | Move back to the beginning of the current word. |
B | Move back to the beginning of the current word and ignore punctuation. |
e | Move forward to the end of the current word. |
ge | Move backward to the end of the previous word. |
) | Move forward to the start of the next sentence. |
( | Move backward to the start of the previous sentence. |
Moving within a Line
These keys allow users to move the cursor to specific positions on the screen or the current line:
Key | Description |
---|---|
0 | Move to the beginning of the current line. |
$ | Move to the end of the current line. |
^ | Move to the first non-whitespace character. |
Moving by Paragraph
Use the following commands to move between paragraphs:
Key | Description |
---|---|
} | Move to the beginning of the next paragraph. |
{ | Move to the beginning of the previous paragraph. |
Search for Patterns
Vim has a built-in search feature that allows users to move to matching text using simple pattern-based commands:
Key | Description |
---|---|
/ followed by the search pattern | Search forward for a string or pattern. |
? followed by the search pattern | Search backward for a string or pattern. |
n | Repeat the last search in the same direction. |
N | Repeat the last search in the opposite direction. |
* | Search forward for the word under the cursor. |
# | Search backward for the word under the cursor. |
Note: Searching is one of the most common actions in Vim. Learn the basics and explore different strategies and advanced search commands in Vim.
Moving Across the File
Use the following commands to move to the beginning, end, or a specific line of the file:
Key | Description |
---|---|
gg | Move to the beginning of the file. |
G | Move to the end of the file. |
nG | Move to line n (e.g., 42G jumps to line 42). |
Moving by Half or Full Pages
To move through large documents, you can move by half or full pages in one go:
Key | Description |
---|---|
Ctrl-d | Scroll down half a screen. |
Ctrl-u | Scroll up half a screen. |
Ctrl-f | Scroll forward one full screen. |
Ctrl-b | Scroll backward one full screen. |
Note: When a command is written as Ctrl-u
, it means press and hold the Ctrl key and press u at the same time.
Editing Text Using Keybindings
Text editing in Vim is primarily performed in Normal mode. Many of the commands are simple key combinations of operators, motions, and optional counts.
Deleting Text
Use these commands to remove characters, words, lines, or sections of text:
Keybinding | Description |
---|---|
x | Delete the character under the cursor. |
X | Delete the character before the cursor. |
dd | Delete the entire current line. |
d$ | Delete from the cursor to the end of the line. |
d0 | Delete from the cursor to the beginning of the line. |
d4w | Delete the next 4 words. |
Note: Explore our detailed guide to learn the various ways to delete lines in Vim.
Copying (Yanking) Text
Copying in Vim is called yanking. These commands copy lines, words, or ranges of text into the unnamed register, which is Vim's default clipboard:
Keybinding | Description |
---|---|
yy | Copy the current line. |
y$ | Copy from the cursor to the end of the line. |
y0 | Copy from the cursor to the beginning of the line. |
y2j | Copy the current line and the 2 lines below it. |
yw | Copy the next word. |
Pasting Text
After copying or deleting text, use the following commands to paste relative to the cursor position:
Keybinding | Description |
---|---|
p | Paste after the cursor or the current line. |
P | Paste before the cursor or the current line. |
Changing Text
Use the following commands to delete the selected text and switch to Insert Mode to enter replacement content.
Keybindings | Description |
---|---|
cw | Change the word from the cursor onward. |
C | Change from the cursor to the end of the line. |
cc | Change the entire current line. |
ci" | Change everything inside the double quotes. |
ci( | Change everything inside the parentheses. |
Undo, Redo, and Repeat
Undo mistakes, redo changes, or repeat the last command with these keybindings:
Keybindings | Descriptions |
---|---|
u | Undo the last change. |
Ctrl-r | Redo the last undone change. |
. | Repeat the last command exactly as it was run. |
Note: The following text provides detailed insight into how undo, redo, and repeat commands in Vim work.
Indenting
The following commands allow you to adjust or auto-format indentation to, for example, control code alignment:
Keybinding | Description |
---|---|
>> | Indent the current line to the right. |
<< | Indent the current line to the left. |
= followed by a motion | Auto-indent a range (e.g., gg=G indents the entire file). |
Keybindings in Insert Mode
Insert mode allows users to type and edit text directly, just like in a regular text editor. You enter Insert mode from Normal mode and return by pressing Esc.
Access Insert Mode
Vim provides multiple ways to enter Insert mode, which differ based on the cursor position:
Keybindings | Description |
---|---|
i | Insert before the cursor. |
I | Insert at the beginning of the line. |
a | Append after the cursor. |
A | Append at the end of the line. |
o | Open a new line below and enter Insert Mode. |
O | Open a new line above and enter Insert Mode. |
Deleting in Insert Mode
The following keys enable users to delete or adjust text while in Insert Mode:
Keybinding | Description |
---|---|
Backspace | Delete the character before the cursor. |
Ctrl-w | Delete the previous word. |
Ctrl-u | Delete from the cursor to the start of the line. |
Ctrl-h | Alternative to Backspace. |
Completion and Assistance
Use these keys for autocompletion based on existing words or keywords.
Keybinding | Description |
---|---|
Ctrl-n | Autocomplete the next match. |
Ctrl-p | Autocomplete the previous match. |
Exit Insert Mode
Use the following commands to return to Normal Mode after editing the content in Insert Mode:
Keybinding | Description |
---|---|
Esc | Return to Normal Mode. |
Ctrl-[ | Alternative to Esc. |
Keybindings in Visual Mode
Visual mode is used for selecting and manipulating blocks of text. Once the text is selected, you can apply most operators (like delete, copy, or change) directly to the selection. This mode is handy when motions are not precise enough to define the editing range.
Entering Visual Mode
There are three types of Visual mode depending on the range of text you need to edit. Use one of the following commands to enter Visual mode, depending on the range of text you need to edit:
Keybindings | Description |
---|---|
v | Start character-wise selection. |
V | Start line-wise selection. |
Ctrl-v | Start block-wise selection. |
Editing in Visual Mode
Once you've made a selection, apply regular Normal mode commands directly to the highlighted area:
Keybindings | Description |
---|---|
d | Delete the selected text. |
y | Copy the selected text. |
c | Change the selected text and enter Insert Mode. |
> | Indent the selection. |
< | Unindent the selection. |
~ | Toggle the case of the selected characters. |
: | Enter command-line mode only for the selected text. |
These are the same operators used in Normal mode, but now act only on the selected text. There is no need to use counters and motion components.
Keybindings in Command-Line Mode
Command-line mode in Vim is used to enter commands for saving, quitting, searching, replacing text, and more.
Entering Command-Line Mode
While in Normal Mode, enter the following command to switch to Command-line Mode:
Keybinding | Description |
---|---|
: | Enter command-line mode. |
/ | Start a forward search. |
? | Start a reverse search. |
Common Commands
These are the most frequently used command-line operations:
Keybinding | Description |
---|---|
:w | Save the current file. |
:q | Quit Vim. |
:wq | Save the file and quit. |
:q! | Quit without saving changes. |
:e <filename> | Open a new file. |
:help | Open Vim help. |
Find and Replace
In Vim, users can quickly find and replace all occurrences of a pattern using a single command:
Keybinding | Description |
---|---|
:s/old_text/new_text/ | Replace the first occurrence of old_text with new_text in the current line. |
:s/old_text/new_text/g | Replace all old_text with new_text in the current line. |
:%s/<strong><code>old_text /new_text /g | Replace all old_text with new_text in the entire file. |
:%s/<strong><code>old_text /new_text /gc | Replace all instances of old_text with new_text throughout the entire file, prompting confirmation for each change. |
Editing and History
These keys help you navigate and edit within the command-line prompt itself:
Keybindings | Description |
---|---|
q: | Open the command-line history window. |
q/ | Open the search history window. |
arrow up key / arrow down key | Cycle through previous command-line history. |
Ctrl-b | Move to the beginning of the command line. |
Ctrl-e | Move to the end of the command line. |
Ctrl-w | Delete the previous word. |
Ctrl-u | Delete from the cursor to the beginning of the line. |
Ctrl-r" | Insert the last copied text from the unnamed register into the command line. |
Keybindings in Replace Mode
Replace mode in Vim is similar to Insert mode, but instead of inserting new text and shifting existing content forward, it overwrites characters at the cursor position. It is useful when you want to substitute characters directly without deleting or shifting the line.
A typical Replace mode workflow includes the following actions:
- Start in Normal mode at the cursor position.
- Press
R
to enter Replace mode. - Type a replacement string to overwrite existing text.
- Press Esc to return to Normal mode.
Use Replace mode for quick corrections to avoid the need to delete text in Normal mode and then reinsert it in Insert mode.
Keybindings in Select Mode
Select mode is not available by default in many terminal-based Vim setups. It is similar to Visual mode, but instead of waiting for you to issue an operator command, for example, d
or y
, typed characters immediately replace the selected text, just like in GUI-based text editors.
Feature | Visual Mode | Select Mode |
---|---|---|
Selection Behaviour | Waits for an operator (d , y , etc.) | Typing replaces the selected text. |
Mode Shown in Status | --Visual-- | --Select-- |
Default in Vim CLI | Yes | No |
Select mode is rarely used in command-line Vim, and many users may never encounter it in daily workflows. However, it's helpful to understand how it differs from other modes.
Programing-Specific Keybindings
Vim keybindings are particularly useful when writing or editing code. Many of these work out of the box in Normal or Visual mode, while others rely on optional features like filetype detection or external plugins.
Source code is often structured around text objects, functions, code blocks, and indentation, which Vim can operate on directly. The following keybindings help users navigate codebases and perform repetitive edits with precision.
Text Objects
Text objects allow you to act on logical units of code like words, quotes, parentheses, and blocks:
Keybinding | Description |
---|---|
ci\" | Change the content inside the double quotes. |
di{ | Delete the content inside the curly brackets. |
va( | Visually select a block, including the parentheses. |
yi' | Copy the content inside the single quotes. |
da[ | Delete the entire bracketed expression, including the brackets. |
Function and Block Navigation
Use the following keybindings to move between functions or block structures in code:
Keybinding | Description |
---|---|
[[ | Jump to the beginning of the previous function. |
]] | Jump to the beginning of the next function. |
{ and } | Jump between code blocks (based on blank lines or indentation). |
% | Jump to the matching bracket, parentheses, or brace. |
Folding Code
Code folding lets you collapse or expand code blocks to focus on important sections. This is especially useful in large files or while debugging.
Keybinding | Description |
---|---|
za | Toggle folder under the cursor. |
zc | Close fold. |
zo | Open fold. |
zM | Close all folds. |
zR | Open all folds. |
Macros
Vim allows users to record short sequences of actions to automate repetitive tasks, so-called macros.
Keybinding | Description |
---|---|
q <register> | Start recording a macro into the given register. For example, enter qa to record a macro in the a register. |
q | Stop recording the macro. |
@ <register> | Play the macro stored in the listed register. For example, @a plays the macro stored in the a register. |
@@ | Repeat the last played macro. |
Note: When working with code, it is sometimes helpful to display line numbers. This guide explains how to show or hide line numbers in Vim.
Additional Vim Keybindings - Quick Reference
This quick-reference table summarizes additional useful keybindings that do not appear in the main instructional sections above.
Category | Keybinding | Description |
---|---|---|
Navigation | H | Move to the top of the screen. |
M | Move to the middle of the screen. | |
L | Move to the bottom of the screen. | |
zz | Center the current line in the middle of the screen. | |
zt | Move the current line to the top of the screen. | |
zb | Move the current line to the bottom of the screen. | |
Ctrl-y | Scroll the screen up one line. | |
Ctrl-e | Scroll the screen down one line. | |
Formatting | gg=G | Auto-indent the entire file. |
J | Join the current line with the next one and remove the new line. | |
~ | Toggle the case (uppercase/lowercase) of the character under the cursor. | |
g~w | Toggle case of the next word. | |
guu | Make the current line lowercase. | |
gUU | Make the current line uppercase. | |
gqq | Reformat the current line to fit within the textwidth setting. | |
Insert Mode | Ctrl-o | Temporarily switch to Normal mode for one command. |
Ctrl-r"a | Insert content from register a. | |
Ctrl-x Ctrl-l | Insert a whole line completion. | |
Ctrl-x Ctrl-f | Insert a filename completion from a file system. | |
Visual Mode | gv | Reselect the last visual selection. |
r <character> | Replace all selected characters with the specified character. For example, to replace each selected character with 3, enter r3 . | |
= | Auto-indent the selection. | |
: | Open the command-line mode with a selection range prefilled. | |
gU | Convert the selection to uppercase. | |
gu | Convert the selection to lowercase. | |
Replace Mode | R | Enter Replace mode. |
r <character> | Replace the character under the cursor. | |
Text Objects | vi( | Select inside the parentheses. |
va' | Select the string around the single quotes. | |
va" | Select the string around the double quotes. | |
ci] | Change inside the square brackets. | |
di> | Delete inside the angle brackets. | |
Macros | :let @a='...' | Define a macro directly using a command |
:reg | Show contents of all registers. | |
Search and Patterns | * | Search forward for the word under the cursor. |
# | Search backward for the word under the cursor. | |
:noh | Clear the search highlight. | |
:vimgrep | Search across multiple files using Vim's grep. | |
:cn | Jump to the next match in the quickfix list. | |
:cp | Jump to the previous match in the quickfix list. | |
Command-Line | :x | Save and quit if changes exist. |
:! <command> | Run a shell command without leaving Vim. | |
:r ! <command> | Insert the output of a shell command below the current line. | |
:cd <directory_name> | Change the working directory inside Vim. | |
:pwd | Print the current working directory. | |
:ls | List all open buffers. | |
:sp <file_name> | Open the file in a horizontal split. | |
:vs <file_name> | Open the file in a vertical split. | |
:tabnew <file_name> | Open a new tab and load the specified file. |
Note: Vim has a steep learning curve, but you can enhance your editing experience with syntax highlighting and customizable interface colors. Read our guide to learn how to configure Vim color schemes.
Conclusion
Vim is not known as a beginner-friendly text editor. Use the commands outlined in this guide to practice, and over time, many of these keybindings will become part of your muscle memory.
If you work with code, learning Git from the command line pairs naturally with Vim.