← Back to DevBytes

Vim Keyboard Shortcuts: Complete Guide

Introduction to Vim Keyboard Shortcuts

Vim (Vi Improved) is one of the most powerful and widely used text editors in the developer ecosystem. Originally built as an enhanced version of the classic Unix editor vi, Vim relies almost entirely on keyboard shortcuts for navigation and editing. This design philosophy allows developers to keep their hands on the keyboard, eliminating the need to constantly switch between keyboard and mouse. Whether you are editing configuration files on a remote server, writing code locally, or customizing your development environment, mastering Vim shortcuts can dramatically improve your productivity.

What Makes Vim Different

Unlike most modern text editors that operate in a single mode where typing inserts text directly, Vim is a modal editor. This means the behavior of your keystrokes changes depending on the mode you are in. While this concept can feel unfamiliar at first, it is the foundation of Vim's efficiency. Once you internalize the modal workflow, you will find that complex editing tasks become fast, precise, and repeatable.

Why Vim Shortcuts Matter

Learning Vim shortcuts is an investment that pays dividends throughout your entire career. Here are the key reasons why Vim shortcuts matter:

Understanding Vim Modes

Before diving into specific shortcuts, it is essential to understand Vim's modes. Every shortcut behaves differently depending on the active mode. The four primary modes are:

Normal Mode

This is the default mode when you open Vim. In Normal mode, keystrokes are interpreted as commands rather than text input. You use this mode for navigation, deletion, copying, pasting, and executing other editing operations. Press Esc at any time to return to Normal mode.

Insert Mode

In Insert mode, Vim behaves like a conventional text editor. Every key you press is inserted as text into the buffer. You enter Insert mode from Normal mode using commands like i, a, or o.

Visual Mode

Visual mode allows you to select text before performing an operation. You can select characters, lines, or blocks. Once text is selected, you can apply operators like delete, copy, or replace.

Command-Line Mode

Command-line mode is used for executing Ex commands, searching, and configuring Vim settings. You enter this mode by typing : in Normal mode. Commands are executed by pressing Enter.

Essential Movement Shortcuts

Movement is the first skill every Vim user should master. Efficient navigation eliminates the need for arrow keys and mouse scrolling. All of the following shortcuts are used in Normal mode.

Basic Cursor Movement

h    Move cursor left
j    Move cursor down
k    Move cursor up
l    Move cursor right

These keys are positioned on the home row, making them the most ergonomic way to move around. While arrow keys also work, using h, j, k, l is the recommended approach.

Word Movement

w    Move forward to the start of the next word
b    Move backward to the start of the previous word
e    Move forward to the end of the current word
W    Move forward to the start of the next WORD (whitespace-delimited)
B    Move backward to the start of the previous WORD
E    Move forward to the end of the current WORD

The difference between a word and a WORD is important. A word consists of letters, digits, and underscores, while a WORD is any sequence of non-blank characters separated by whitespace.

Line Movement

0    Move to the beginning of the line
^    Move to the first non-blank character of the line
$    Move to the end of the line
gg   Move to the first line of the file
G    Move to the last line of the file
:n   Move to line number n (e.g., :42 goes to line 42)

Screen Movement

Ctrl-f    Scroll forward one full screen
Ctrl-b    Scroll backward one full screen
Ctrl-d    Scroll down half a screen
Ctrl-u    Scroll up half a screen
H         Move cursor to the top of the screen
M         Move cursor to the middle of the screen
L         Move cursor to the bottom of the screen

Character Search Within a Line

fx    Move cursor to the next occurrence of character x
Fx    Move cursor to the previous occurrence of character x
tx    Move cursor to just before the next occurrence of x
Tx    Move cursor to just after the previous occurrence of x
;     Repeat the last f, F, t, or T command
,     Repeat the last f, F, t, or T command in reverse

For example, if your cursor is at the beginning of the line const greeting = "Hello, World!"; and you press f", the cursor jumps directly to the first quotation mark. This is extremely useful for navigating code.

Entering and Exiting Insert Mode

There are multiple ways to enter Insert mode, each suited to a different editing scenario. Choosing the right entry point saves keystrokes and keeps your workflow smooth.

i    Insert text before the cursor
a    Insert text after the cursor
I    Insert text at the beginning of the line
A    Insert text at the end of the line
o    Open a new line below the cursor and enter Insert mode
O    Open a new line above the cursor and enter Insert mode
s    Delete the character under the cursor and enter Insert mode
S    Delete the entire line and enter Insert mode
cc   Change the entire line (delete line and enter Insert mode)

For example, if you want to add a semicolon at the end of the current line, press A followed by ;. This is faster than moving to the end of the line with $ and then pressing a.

Editing Shortcuts

Delete and Yank (Copy)

x    Delete the character under the cursor
dd   Delete the current line
dw   Delete from the cursor to the start of the next word
de   Delete from the cursor to the end of the current word
d$   Delete from the cursor to the end of the line
D    Delete from the cursor to the end of the line (same as d$)
yy   Yank (copy) the current line
yw   Yank from the cursor to the start of the next word
y$   Yank from the cursor to the end of the line

Paste (Put)

p    Paste the yanked or deleted text after the cursor
P    Paste the yanked or deleted text before the cursor

In Vim, deleted text is stored in the same register as yanked text, meaning you can use dd to cut a line and p to paste it elsewhere.

Undo and Redo

u      Undo the last change
Ctrl-r Redo the last undone change
U      Restore the current line to its original state

Replace and Change

r    Replace the character under the cursor with the next typed character
R    Enter Replace mode (overwrites characters as you type)
cw   Change from the cursor to the end of the current word
c$   Change from the cursor to the end of the line
C    Change from the cursor to the end of the line (same as c$)
ci"  Change the text inside double quotes
ci(  Change the text inside parentheses
cit  Change the text inside an HTML tag

The ci" command is one of the most powerful shortcuts in Vim. If your cursor is anywhere inside "Hello, World!", pressing ci" deletes the text between the quotes and puts you in Insert mode to type a replacement. This works with any pair of delimiters: (), [], {}, '', and "".

Text Objects and Operators

Vim's text object system is what truly sets it apart from other editors. The general pattern is [operator][text object]. Operators include d (delete), c (change), y (yank), and v (visual select). Text objects define the scope of the operation.

aw    A word (includes trailing whitespace)
iw    Inner word (no trailing whitespace)
as    A sentence
is    Inner sentence
ap    A paragraph
ip    Inner paragraph
a"    A double-quoted string (includes the quotes)
i"    Inner double-quoted string (excludes the quotes)
a'    A single-quoted string
i'    Inner single-quoted string
a(    A parenthesized block (includes parentheses)
i(    Inner parenthesized block (excludes parentheses)
a[    A bracketed block
i[    Inner bracketed block
a{    A braced block
i{    Inner braced block
at    An HTML/XML tag block (includes tags)
it    Inner HTML/XML tag block (excludes tags)

Here are some practical examples of combining operators with text objects:

daw    Delete a word and the space after it
ciw    Change the inner word under the cursor
ya(    Yank the text inside parentheses including the parentheses
dat    Delete an entire HTML tag block
vit    Visually select the inner content of an HTML tag

Consider the following HTML snippet:

<div class="container">
  <p>Hello, World!</p>
</div>

If your cursor is on the word "Hello" and you press cit, Vim deletes "Hello, World!" and enters Insert mode, allowing you to type replacement content while keeping the <p></p> tags intact.

Search and Replace

Searching

/pattern    Search forward for pattern
?pattern    Search backward for pattern
n           Repeat the search in the same direction
N           Repeat the search in the opposite direction
*           Search forward for the word under the cursor
#           Search backward for the word under the cursor

For example, to search for all occurrences of function in your file, type /function and press Enter. Press n to jump to the next match and N to go to the previous match.

Search and Replace

:%s/old/new/g       Replace all occurrences of "old" with "new" in the entire file
:%s/old/new/gc      Replace all occurrences with confirmation prompt
:s/old/new/g        Replace all occurrences on the current line
:5,10s/old/new/g    Replace all occurrences between lines 5 and 10
:%s/old/new/gi      Replace all occurrences, case-insensitive

The % symbol represents the entire file. The g flag means global (all occurrences on each line, not just the first). The c flag adds a confirmation prompt for each replacement, which is useful when you want to review each change.

Here is a practical example. Suppose you have a JavaScript file where you want to rename a variable from oldName to newName throughout the file:

:%s/oldName/newName/g

If you want to be cautious and confirm each replacement, add the c flag:

:%s/oldName/newName/gc

Vim will prompt you with replace with newName (y/n/a/q/l/^E/^Y)? for each occurrence. Press y to replace, n to skip, a to replace all remaining occurrences, or q to quit.

Visual Mode Shortcuts

Visual mode is used for selecting text before applying an operation. There are three types of Visual mode:

v       Enter character-wise Visual mode
V       Enter line-wise Visual mode
Ctrl-v  Enter block-wise Visual mode (column selection)

Character-wise Visual Mode

Press v to start selecting characters. Move the cursor to extend the selection. Once text is selected, you can apply any operator:

v        Start character-wise selection
vew      Select from cursor to end of next word
d        Delete the selected text
y        Yank (copy) the selected text
>        Indent the selected text
<        Unindent the selected text
~        Toggle case of selected text

Line-wise Visual Mode

Press V to select entire lines. This is useful when you want to operate on complete lines of code:

V        Start line-wise selection
Vjj      Select the current line and the next two lines
d        Delete the selected lines
y        Yank the selected lines
>        Indent the selected lines

Block-wise Visual Mode

Block-wise Visual mode is one of Vim's most powerful features. It allows you to select a rectangular block of text, which is invaluable for editing columnar data or adding prefixes to multiple lines simultaneously.

Ctrl-v       Start block-wise selection

Here is a practical example. Suppose you have the following list and you want to add a comment marker # at the beginning of each line:

apple
banana
cherry
date

Follow these steps:

1. Place cursor on the "a" of "apple"
2. Press Ctrl-v to enter block-wise Visual mode
3. Press 3j to extend the selection down to "date"
4. Press I (capital i) to enter Insert mode before the block
5. Type # followed by a space
6. Press Esc

The # will be inserted at the beginning of all selected lines simultaneously.

Working with Multiple Files

Buffers

A buffer is Vim's in-memory representation of a file. You can have multiple buffers open simultaneously.

:e filename      Open a file in a new buffer
:ls              List all open buffers
:bnext  or :bn   Switch to the next buffer
:bprev  or :bp   Switch to the previous buffer
:bd              Delete (close) the current buffer
:b 2             Switch to buffer number 2
:b filename      Switch to buffer matching filename

Windows

Windows are viewports into buffers. You can split the screen to view multiple buffers or different parts of the same buffer.

:split   or :sp     Split the window horizontally
:vsplit  or :vs     Split the window vertically
Ctrl-w h            Move cursor to the window on the left
Ctrl-w j            Move cursor to the window below
Ctrl-w k            Move cursor to the window above
Ctrl-w l            Move cursor to the window on the right
Ctrl-w =            Equalize the size of all windows
Ctrl-w +            Increase window height
Ctrl-w -            Decrease window height
Ctrl-w >            Increase window width
Ctrl-w <            Decrease window width
:close  or :q       Close the current window

Tabs

Tabs in Vim are collections of windows, not single files. Each tab can contain multiple split windows.

:tabnew filename    Open a file in a new tab
:tabclose           Close the current tab
:tabnext   or :tabn Go to the next tab
:tabprev   or :tabp Go to the previous tab
:tabfirst           Go to the first tab
:tablast            Go to the last tab
gt                  Go to the next tab (Normal mode)
gT                  Go to the previous tab (Normal mode)

Advanced Shortcuts

Marks

Marks allow you to save cursor positions and jump back to them later. This is useful when you need to navigate between different parts of a large file.

ma    Set mark "a" at the current cursor position
'a    Jump to the line of mark "a"
`a    Jump to the exact position of mark "a"
''    Jump to the line of the last jump
``    Jump to the exact position of the last jump
:marks List all marks

Lowercase marks are local to the current buffer, while uppercase marks (e.g., mA) are global and can be accessed from any buffer.

Registers

Registers are Vim's clipboard system. There are multiple registers for different purposes, and you can explicitly specify which register to use.

"ayy   Yank the current line into register "a"
"ap    Paste the contents of register "a"
"byw   Yank a word into register "b"
"bp    Paste the contents of register "b"
"+yy   Yank the current line to the system clipboard
"+p    Paste from the system clipboard
:reg   Display the contents of all registers

The " character is used to specify a register before an operator. The + register corresponds to the system clipboard on most systems, allowing you to copy and paste between Vim and other applications.

Macros

Macros allow you to record a sequence of keystrokes and replay them. This is incredibly powerful for repetitive editing tasks.

qa     Start recording a macro into register "a"
...    Perform your editing actions
q      Stop recording
@a     Execute the macro stored in register "a"
@@     Execute the last executed macro again
10@a   Execute the macro 10 times

Here is a practical example. Suppose you have a list of names that you want to wrap in quotes and add a comma at the end:

apple
banana
cherry
date

Follow these steps to create a macro:

1. Place cursor on the first line ("apple")
2. Press qa to start recording into register "a"
3. Press I to enter Insert mode at the beginning of the line
4. Type " (a double quote)
5. Press Esc
6. Press A to enter Insert mode at the end of the line
7. Type ",
8. Press Esc
9. Press j to move to the next line
10. Press q to stop recording
11. Press 3@a to apply the macro to the remaining three lines

The result will be:

"apple",
"banana",
"cherry",
"date",

The Dot Command

The . (dot) command repeats the last change made in Normal mode. This is one of the most powerful and frequently used shortcuts in Vim.

.    Repeat the last change

For example, if you press dw to delete a word, pressing . will delete the next word as well. If you use ciw to change a word, pressing . will change the next word with the same replacement text. Combining the dot command with movements like n (next search match) creates a powerful editing workflow.

Customizing Vim Shortcuts

Vim is highly customizable. You can create your own shortcuts and modify existing ones through your .vimrc configuration file.

Basic Key Mappings

" Map leader key to space
let mapleader = " "

" Save file with leader + w
nnoremap <leader>w :w<CR>

" Quit file with leader + q
nnoremap <leader>q :q<CR>

" Save and quit with leader + x
nnoremap <leader>x :x<CR>

" Split window vertically with leader + vs
nnoremap <leader>vs :vsplit<CR>

" Split window horizontally with leader + sp
nnoremap <leader>sp :split<CR>

" Navigate between splits using leader + arrow keys
nnoremap <leader>h <C-w>h
nnoremap <leader>j <C-w>j
nnoremap <leader>k <C-w>k
nnoremap <leader>l <C-w>l

Understanding Mapping Modes

nmap    Normal mode mapping (recursive)
vmap    Visual mode mapping (recursive)
imap    Insert mode mapping (recursive)
cmap    Command-line mode mapping (recursive)

nnoremap    Normal mode mapping (non-recursive)
vnoremap    Visual mode mapping (non-recursive)
inoremap    Insert mode mapping (non-recursive)
cnoremap    Command-line mode mapping (non-recursive)

It is a best practice to use the non-recursive variants (nnoremap, vnoremap, etc.) to avoid unexpected behavior caused by recursive mappings.

Useful Custom Mappings Example

" Clear search highlighting with leader + n
nnoremap <leader>n :nohlsearch<CR>

" Toggle line numbers with leader + ln
nnoremap <leader>ln :set number!<CR>

" Yank to system clipboard with leader + y
vnoremap <leader>y "+y
nnoremap <leader>y "+y

" Paste from system clipboard with leader + p
nnoremap <leader>p "+p
vnoremap <leader>p "+p

" Move selected lines up and down
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv

Best Practices for Learning and Using Vim

Start with the Basics

Do not try to learn every shortcut at once. Begin with the essentials: movement (h, j, k, l), entering and exiting Insert mode (i, a, o, Esc), saving and quitting (:w, :q, :wq), and basic editing (x, dd, yy, p, u). Once these become muscle memory, gradually add more shortcuts to your repertoire.

Use Vimtutor

Vim ships with an interactive tutorial called vimtutor. Run it from your terminal by typing:

vimtutor

This 30-minute tutorial walks you through the fundamental shortcuts with hands-on exercises. It is the single best starting point for new Vim users.

Disable Arrow Keys Temporarily

To force yourself to learn the h, j, k, l movement keys, consider disabling the arrow keys in your .vimrc:

nnoremap <Up> <Nop>
nnoremap <Down> <Nop>
nnoremap <Left> <Nop>
nnoremap <Right> <Nop>

This will feel uncomfortable at first, but it will accelerate your transition to Vim-style navigation.

Learn the Command Language

Vim shortcuts follow a consistent grammar: [count][operator][text object or motion]. Once you understand this pattern, you can combine commands intuitively rather than memorizing them individually. For example:

2dd     Delete 2 lines (count=2, operator=d, text object=d which means line)
d3w     Delete 3 words (operator=d, count=3, motion=w)
y2j     Yank the current line and the next 2 lines
c2w     Change 2 words

Practice Deliberately

Identify one new shortcut each day and consciously use it in your work. Over time, these shortcuts will become second nature. Keep a cheat sheet handy, and refer to it when you find yourself doing something inefficiently.

Avoid Bad Habits

Use Plugins to Extend Vim

While Vim is powerful on its own, plugins can further enhance your workflow. Some popular plugins include:

If you use a plugin manager like vim-plug, your .vimrc might look like this:

call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-fugitive'
Plug 'dense-analysis/ale'
call plug#end()

After adding these lines, run :PlugInstall in Vim to install the plugins.

Quick Reference Cheat Sheet

Here is a condensed cheat sheet of the most commonly used Vim shortcuts for quick reference:

Saving and Quitting

:w              Save the file
:q              Quit Vim
:wq   or :x     Save and quit
:q!             Quit without saving
ZZ              Save and quit (Normal mode shortcut)
ZQ              Quit without saving (Normal mode shortcut)

Movement

h j k l         Left, down, up, right
w b e           Next word, previous word, end of word
0 ^ $           Start of line, first non-blank, end of line
gg G            First line, last line
Ctrl-d Ctrl-u   Half page down, half page up
Ctrl-f Ctrl-b   Full page down, full page up

Editing

i a o           Insert before, after, new line below
I A O           Insert at start, end, new line above
x dd yy p       Delete char, delete line, yank line, paste
u Ctrl-r        Undo, redo
.               Repeat last change

Search

/pattern        Search forward
?pattern        Search backward
n N             Next match, previous match
* #             Search word under cursor forward, backward
:%s/old/new/g   Replace all in file

Conclusion

Mastering Vim keyboard shortcuts is a journey that transforms how you interact with text and code. The modal editing paradigm, combined with the powerful operator-and-text-object grammar, gives you a language for editing that is both expressive and efficient. While the initial learning curve can be steep, the long-term productivity gains are substantial. Start with the fundamentals, practice deliberately, and gradually incorporate advanced techniques like macros, registers, and custom mappings into your workflow. Remember that Vim is not just an editor but a skill that compounds over time. Every shortcut you learn makes you faster, and every workflow you optimize stays with you across every system where Vim is available. By following the best practices outlined in this guide and building your muscle memory one shortcut at a time, you will soon find yourself editing text with a speed and precision that simply is not possible with conventional editors.

— Ad —

Google AdSense will appear here after approval

← Back to all articles