Fundamentals 9 min read

Master Vim: Explore the Three Modes and Essential Commands

This guide explains Vim's three core modes—command, insert, and command-line—detailing essential navigation, editing, and configuration commands, address ranges, search and replace syntax, window management, visual mode operations, and key settings for efficient text editing.

Raymond Ops
Raymond Ops
Raymond Ops
Master Vim: Explore the Three Modes and Essential Commands

Vim's Three Modes

1. Command Mode

When Vim starts you are in command mode, where you can move the cursor, delete, copy, paste, and issue commands.

Common commands: i – switch to insert mode; x – delete character under cursor; : – enter command-line mode; dd – delete current line; yy – yank (copy) current line; :w – write file; :q – quit; :e! – reload file discarding changes.

2. Insert Mode

Press i in command mode to enter insert mode and type text freely. Press Esc to return to command mode.

3. Command-line Mode

Press : in command mode to enter command-line mode, where you can type one‑ or multi‑character commands such as w (write), q (quit), wq (write and quit), q! (force quit), x (write and quit), r file (read another file), etc.

Address Ranges and Operations

Use symbols like # , . , $ , % to specify lines. Examples: #,# – range from line # to line #; . – current line; .,$-1 – from current line to the penultimate line; % – whole file. Commands d , y , r work with these ranges.

Search and Replace

Search with

/pattern/

, range search with

/pat1/,/pat2/

, etc. Replace syntax:

<code>s/old/new/flags</code>

Common flags: i – ignore case; g – global; gc – global with confirmation.

Vim Features

Configuration files:

/etc/vimrc

(global) and

~/.vimrc

(user). Common settings:

<code>set nu      " show line numbers
set nonu    " hide line numbers
set ic      " ignore case
set noic    " case-sensitive
set ai      " auto indent
set noai    " no auto indent
set paste   " enable paste mode
set nopaste " disable paste mode
set list    " show hidden characters
set nolist  " hide hidden characters
set hlsearch " highlight search
set nohlsearch " disable highlight
syntax on   " enable syntax highlighting
syntax off  " disable syntax highlighting
set fileformat=dos   " Windows line endings
set fileformat=unix  " Unix line endings
set et      " use spaces for tabs
set noet    " use real tabs
set ts=4    " tab stop width
set shiftwidth=4 " indent width
set textwidth=80 " wrap at column 80
set cul     " highlight current line
set nocul   " disable current line highlight
set key=passwd " set encryption key
</code>

Window Management

Split windows:

:split

(horizontal),

:vsplit

(vertical). Switch focus with

Ctrl+w

. Close windows with

:q

, close others with

:only

. Resize with

Ctrl+w +/‑

(height) or

Ctrl+w &gt;/&lt;

(width). Sync scrolling with

set scrollbind

and

set noscrollbind

. Manage tabs:

:tabedit

,

:tabnew

,

gt

,

:tabclose

,

:tabonly

.

Visual Mode

Enter visual mode with v (character), V (line), Ctrl+v (block). Use motion keys to select text, then y to yank, d to delete, p / P to paste. Undo with u , redo with Ctrl+r .

configurationText EditorVimnavigationcommand modeinsert mode
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.