_____                   _             ___     _________  __
 |_   _|__ _ __ _ __ ___ (_)_ __   __ _| \ \   / / ____\ \/ /
   | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | |\ \ / /|  _|  \  /
   | |  __/ |  | | | | | | | | | | (_| | | \ V / | |___ /  \
   |_|\___|_|  |_| |_| |_|_|_| |_|\__,_|_|  \_/  |_____/_/\_\
advanced14 min

Vim Editor: A Beginner's Guide

What is Vim and Why Should You Learn It?

Vim (Vi IMproved) is one of the most established and powerful text editors in the Unix world. It was created in 1991 by Bram Moolenaar as an enhanced version of the vi editor written by Bill Joy in 1976. It comes pre-installed on virtually all Linux and macOS systems, making it extremely valuable to learn. When you SSH into a server, even if no other editor is available, Vim will be there.

Although Vim's learning curve may seem steep, once you grasp the basic commands, your editing speed increases dramatically. Vim enables text editing using only the keyboard without a mouse. Keeping your hands on the keyboard at all times provides both speed and ergonomic advantages in the long run. With thousands of plugins and a powerful configuration system, you can customize Vim to your exact preferences.

Vim Modes

Vim's most distinctive feature is its modal architecture. Unlike other editors, Vim has multiple operating modes, and keys have different functions in each mode. Understanding this concept is the most critical step in learning Vim.

Normal Mode (Default)

When you open Vim, you are in Normal mode. In this mode, keys do not type characters; instead, they function as navigation and editing commands. To return to Normal mode from any other mode, simply press Esc. You should spend most of your time in Normal mode because it is the mode for quickly navigating and manipulating text.

Insert Mode

To type text, you need to enter Insert mode. From Normal mode, you can enter Insert mode with any of the following keys:

  • i: Insert before the cursor position.
  • I: Move to the beginning of the line and enter Insert mode.
  • a: Append after the cursor position.
  • A: Move to the end of the line and enter Insert mode.
  • o: Open a new line below the current line and enter Insert mode.
  • O: Open a new line above the current line and enter Insert mode.

Visual Mode

Used for selecting text. Enter from Normal mode as follows:

  • v: Character-wise selection.
  • V: Line-wise selection.
  • Ctrl+v: Block (rectangular) selection.

Command-Line Mode

Press : in Normal mode to enter Command-line mode. Operations such as saving files, quitting, and search-and-replace are performed in this mode.

Basic Navigation

Cursor Movement

# Basic direction keys (in Normal mode)
h  → Left (←)
j  → Down (↓)
k  → Up (↑)
l  → Right (→)

# Word-level movement
w  → Move to the beginning of the next word
b  → Move to the beginning of the previous word
e  → Move to the end of the word

# Line-level movement
0  → Move to the beginning of the line
$  → Move to the end of the line
^  → Move to the first non-blank character

# File-level movement
gg → Go to the beginning of the file
G  → Go to the end of the file
:n → Go to line n (e.g., :42 goes to line 42)
Ctrl+d → Scroll half page down
Ctrl+u → Scroll half page up

Saving and Quitting

# In Command-line mode (:)
:w          → Save the file
:q          → Quit (if no changes)
:wq         → Save and quit
:x          → Save and quit (same as :wq)
:q!         → Force quit without saving
:w file.txt → Save as a different file name
ZZ          → Save and quit in Normal mode (shortcut)

Text Editing

Delete Commands

# Deleting in Normal mode
x   → Delete the character under the cursor
X   → Delete the character before the cursor
dd  → Delete the entire line
dw  → Delete from cursor to end of word
d$  → Delete from cursor to end of line
d0  → Delete from cursor to beginning of line
dG  → Delete from cursor to end of file
3dd → Delete 3 lines

Copy and Paste

# Yank (copy)
yy  → Copy the entire line
yw  → Copy the word
y$  → Copy from cursor to end of line
3yy → Copy 3 lines

# Paste
p   → Paste after the cursor
P   → Paste before the cursor

# Cut (delete + register)
dd  → Cut the line (you can paste with p after deleting)
x   → Cut the character

Search Operations

# Forward search
/search_text  → Press Enter to search
n             → Go to next result
N             → Go to previous result

# Backward search
?search_text  → Search backward

# Search and replace
:%s/old/new/g      → Replace all occurrences in the file
:%s/old/new/gc     → Ask for confirmation for each replacement
:10,20s/old/new/g  → Replace between lines 10 and 20

Undo and Redo

# In Normal mode
u      → Undo the last action
Ctrl+r → Redo the undone action
.      → Repeat the last editing command

Configuration with .vimrc

You can customize Vim's behavior by editing the .vimrc file in your home directory. Here are useful settings to get started:

" ~/.vimrc - Basic configuration

" Show line numbers
set number

" Relative line numbers
set relativenumber

" Syntax highlighting
syntax on

" Case-insensitive search
set ignorecase
set smartcase

" Highlight search results
set hlsearch
set incsearch

" Tab settings (4 spaces)
set tabstop=4
set shiftwidth=4
set expandtab

" Automatic indentation
set autoindent
set smartindent

" Highlight the cursor line
set cursorline

" Make backspace work properly
set backspace=indent,eol,start

" Always show the status bar
set laststatus=2

Tips for Beginners

  • Use the vimtutor command: Type vimtutor in the terminal to start an interactive Vim tutorial. It takes about 30 minutes and teaches the basics.
  • Learn gradually: Do not try to memorize all commands at once. Start with h, j, k, l, i, Esc, :w, :q.
  • Stay in Normal mode: Be in Normal mode unless you are typing text. This habit will increase your speed.
  • Use the repeat operator: The . key repeats the last editing command and saves incredible amounts of time.
  • Exiting Vim: If you get stuck, press Esc a few times and type :q! to always be able to exit.

Summary

Although Vim may seem complex at first glance, it is a powerful editor that multiplies your text editing speed once you master the basics. With its modal architecture, keyboard-centric navigation, and powerful command set, Vim is an indispensable tool for professional developers and system administrators. When you can comfortably switch between Normal mode, Insert mode, and Command-line mode, you will discover the true power of Vim. Be patient, practice a little every day, and over time Vim will become your most productive editing environment.