Essential Vim Commands and Shell Vi Mode

A practical reference for the most-used vim commands and enabling vi mode in your shell

Essential Vim Commands and Shell Vi Mode

A practical guide to the most commonly used vim commands and shell vi mode. This covers the 20% of commands you’ll use 80% of the time.

Setting Up Vi Mode in Shell

Zsh

Add to ~/.zshrc:

1
2
3
4
5
# Enable vi mode
bindkey -v

# Reduce delay when switching modes
export KEYTIMEOUT=1

Bash

Add to ~/.bashrc:

1
2
# Enable vi mode
set -o vi

Apply and Test

1
2
3
4
5
6
7
8
9
# Reload configuration
source ~/.zshrc   # for zsh
source ~/.bashrc  # for bash

# Test it:
# 1. Type some text
# 2. Press ESC (enter command mode)
# 3. Press k (navigate history)
# 4. Press i (back to insert mode)

Shell Vi Mode - Essential Commands

The Basics

CommandAction
ESCEnter command mode
iEnter insert mode
aInsert after cursor
AInsert at end of line
CommandAction
h, j, k, lLeft, down, up, right
wNext word
bPrevious word
0Start of line
$End of line

Editing

CommandAction
xDelete character
ddDelete line
DDelete to end of line
ccDelete line and insert
uUndo
CommandAction
k / jPrevious / next in history
/textSearch history forward
nNext search result

Essential Vim Commands

Opening Files

1
2
vim filename.txt        # Open file
vim +42 filename.txt   # Open at line 42

The Two Modes You Need

ESC     - Normal mode (navigate and command)
i       - Insert mode (type text)

Saving and Quitting

CommandAction
:wSave
:qQuit
:wq or ZZSave and quit
:q!Quit without saving
CommandAction
h, j, k, lLeft, down, up, right
wNext word
bPrevious word
0Beginning of line
$End of line
ggFirst line
GLast line
:42Go to line 42
Ctrl-d / Ctrl-uScroll down/up

Editing

CommandAction
iInsert before cursor
aInsert after cursor
IInsert at line start
AInsert at line end
oOpen line below
OOpen line above
xDelete character
ddDelete line
DDelete to end of line
ccChange entire line
JJoin line with next
uUndo
Ctrl-rRedo
.Repeat last command

Copy, Cut, Paste

CommandAction
yyCopy line
ywCopy word
y$Copy to end of line
ddCut line
dwCut word
pPaste after
PPaste before
CommandAction
/patternSearch forward
?patternSearch backward
nNext result
NPrevious result
*Search word under cursor

Replace

CommandAction
:s/old/new/Replace first in line
:s/old/new/gReplace all in line
:%s/old/new/gReplace all in file
:%s/old/new/gcReplace with confirmation

Visual Mode (Selection)

CommandAction
vVisual character mode
VVisual line mode
yCopy selection
dDelete selection

Quick Reference Card

Most Common Workflow:

1. Open file:        vim file.txt
2. Navigate:         h,j,k,l or arrow keys
3. Start editing:    i (insert) or a (append)
4. Type your text
5. Exit insert:      ESC
6. Save and quit:    :wq

Essential Shortcuts:

# Navigation
gg       - Top of file
G        - Bottom of file
0        - Start of line
$        - End of line
w / b    - Next/previous word

# Editing
dd       - Delete line
yy       - Copy line
p        - Paste
u        - Undo
.        - Repeat

# Search
/text    - Find 'text'
n        - Next result

# Save/Quit
:w       - Save
:q       - Quit
:wq      - Save and quit
:q!      - Quit without saving

Vim Configuration

Permanent Settings (.vimrc)

Create ~/.vimrc with basics:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
" Essentials
set number          " Line numbers
set cursorline      " Highlight current line
set hlsearch        " Highlight search results
set incsearch       " Search as you type
set ignorecase      " Case insensitive search
set smartcase       " Case sensitive if uppercase used

" Indentation
set tabstop=4       " Tab width
set shiftwidth=4    " Indent width
set expandtab       " Spaces instead of tabs
set autoindent      " Auto indent

" Usability
set showcmd         " Show command
set wildmenu        " Command completion
set showmatch       " Show matching brackets

Runtime Settings

Apply these settings within vim for the current session:

1
2
3
4
5
:se ic                                  " ignore case for search
:se noai                                " turn off auto indent
:se hlsearch                            " highlight search matches
:highlight Comment ctermfg=blue        " change color of commented text
:se syntax                              " enable syntax highlighting

These are shortcuts for :set. Add them to .vimrc to make permanent (without the colon).


Tips for Daily Use

1. Learn the Movement Keys First

Before anything else, get comfortable with h, j, k, l and w, b. Your hands never leave home row.

2. Use . (Dot) Command

The dot command repeats your last change. Super powerful:

cw new<ESC>    - Change word to "new"
w              - Move to next word
.              - Repeat the change

3. Shell Vi Mode Tricks

1
2
3
4
5
# After typing a long command:
ESC     - Enter command mode
0       - Jump to start
w w w   - Move forward 3 words
i       - Insert and edit

4. Don’t Overthink It

Start with these basics. Add more as you need them. You don’t need to master everything at once.


Common Mistakes

Stuck in Insert Mode? Press ESC

Can’t Exit Vim? Type :q! and press Enter

Commands Not Working? Make sure you’re in normal mode (press ESC)

Slow Mode Switching? Add export KEYTIMEOUT=1 to your shell config


Resources


Practice Exercise

Try this workflow:

  1. Enable vi mode: bindkey -v (zsh) or set -o vi (bash)
  2. Type: ls -la /usr/local/bin
  3. Press ESC
  4. Press 0 (go to start)
  5. Press w three times (skip “ls -la”)
  6. Press cw (change word)
  7. Type a new path
  8. Press Enter

The key is practice. Use it daily and it becomes second nature.


Remember: You only need to know 20% of vim to be 80% effective. Start here, expand later.

comments powered by Disqus

© 2025 Santosh Manoharan