Master Vim: Complete Guide to Essential Commands for Linux Users

Vim (Vi Improved) is a powerful, highly configurable text editor that has been the cornerstone of efficient text editing for programmers, system administrators, and power users for decades. Whether you’re editing configuration files on a remote server, writing code, or composing documentation, mastering Vim commands can dramatically increase your productivity.

In this comprehensive guide, we’ll cover everything from basic navigation to advanced text manipulation commands that will transform you from a Vim beginner to a proficient user.

What is Vim?

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. First released in 1991 by Bram Moolenaar, Vim has become one of the most popular text editors in the Linux and Unix world.

Vim is often called a “programmer’s editor,” and is so useful for programming that many consider it an entire IDE. However, it’s not just for programmersβ€”Vim is perfect for all kinds of text editing, from composing email to editing configuration files, writing documentation, and managing system administration tasks.

Why Learn Vim?

  • Available everywhere – Vim or Vi is installed on virtually every Linux/Unix system by default
  • Keyboard-centric – No need to reach for the mouse, increasing editing speed
  • Powerful text manipulation – Complex edits can be done with just a few keystrokes
  • Highly customizable – Extensive plugin ecosystem and configuration options
  • Remote editing friendly – Works perfectly over SSH connections with low bandwidth
  • Efficient for repetitive tasks – Macros and commands can automate complex editing workflows

Installing Vim

Most Linux distributions come with Vim pre-installed. To check if Vim is installed and see the version:

# Check Vim version
vim --version

# Install Vim on Ubuntu/Debian
sudo apt update
sudo apt install vim

# Install Vim on RHEL/CentOS/Fedora
sudo dnf install vim

# Install Vim on Arch Linux
sudo pacman -S vim

Understanding Vim Modes

One of the most important concepts in Vim is its modal editing system. Unlike traditional text editors, Vim operates in different modes, each designed for specific tasks:

1. Normal Mode (Command Mode)

This is the default mode when you open Vim. In Normal mode, keys don’t insert text but execute commands for navigation, deletion, copying, and more. Press Esc to return to Normal mode from any other mode.

2. Insert Mode

This is where you actually type text. Enter Insert mode by pressing i (insert before cursor), a (append after cursor), o (open new line below), or O (open new line above).

3. Visual Mode

Used for selecting text. Enter Visual mode with v (character selection), V (line selection), or Ctrl+v (block selection).

4. Command-Line Mode

Execute Ex commands, search, save files, and quit. Enter this mode by pressing : in Normal mode.

Essential Vim Commands for Beginners

Opening and Saving Files

# Open a file
vim filename.txt

# Open file at specific line number
vim +50 filename.txt

# Open multiple files in tabs
vim -p file1.txt file2.txt file3.txt

Within Vim:

  • :w – Save (write) file
  • :w newfile.txt – Save as new filename
  • :q – Quit (only works if no unsaved changes)
  • :q! – Quit without saving changes
  • :wq or :x – Save and quit
  • :e filename.txt – Open another file
  • :e! – Reload current file, discarding changes

Basic Navigation Commands

Character and Line Movement:

  • h – Move cursor left
  • j – Move cursor down
  • k – Move cursor up
  • l – Move cursor right
  • 0 – Move to beginning of line
  • $ – Move to end of line
  • ^ – Move to first non-blank character of line

Word Movement:

  • w – Move forward to beginning of next word
  • b – Move backward to beginning of previous word
  • e – Move to end of current/next word
  • W, B, E – Same as above but treat hyphenated words as one word

Screen Movement:

  • gg – Go to first line of file
  • G – Go to last line of file
  • 50G or :50 – Go to line 50
  • Ctrl+f – Page forward
  • Ctrl+b – Page backward
  • Ctrl+d – Half page down
  • Ctrl+u – Half page up
  • H – Move to top of screen
  • M – Move to middle of screen
  • L – Move to bottom of screen

Editing Commands

Entering Insert Mode

  • i – Insert before cursor
  • I – Insert at beginning of line
  • a – Append after cursor
  • A – Append at end of line
  • o – Open new line below current line
  • O – Open new line above current line
  • s – Delete character under cursor and enter insert mode
  • S – Delete entire line and enter insert mode

Deleting Text

  • x – Delete character under cursor
  • X – Delete character before cursor
  • dw – Delete from cursor to end of word
  • dd – Delete entire line
  • D or d$ – Delete from cursor to end of line
  • d0 – Delete from cursor to beginning of line
  • dG – Delete from current line to end of file
  • dgg – Delete from current line to beginning of file
  • 5dd – Delete 5 lines starting from current line

Copying (Yanking) and Pasting

  • yy or Y – Yank (copy) entire line
  • yw – Yank word
  • y$ – Yank from cursor to end of line
  • 5yy – Yank 5 lines
  • p – Paste after cursor/below current line
  • P – Paste before cursor/above current line

Undo and Redo

  • u – Undo last change
  • U – Undo all changes on current line
  • Ctrl+r – Redo (undo the undo)
  • . – Repeat last command (extremely powerful!)

Advanced Text Manipulation Commands

Search and Replace

Searching:

  • /pattern – Search forward for pattern
  • ?pattern – Search backward for pattern
  • n – Go to next search match
  • N – Go to previous search match
  • * – Search forward for word under cursor
  • # – Search backward for word under cursor

Search and Replace:

# Replace first occurrence on current line
:s/old/new/

# Replace all occurrences on current line
:s/old/new/g

# Replace all occurrences in entire file
:%s/old/new/g

# Replace with confirmation prompt
:%s/old/new/gc

# Replace only in lines 10-20
:10,20s/old/new/g

# Case-insensitive search and replace
:%s/old/new/gi

Text Transformation Commands

Change Case:

# Change character to uppercase/lowercase
~

# Change word to uppercase
gUw

# Change word to lowercase
guw

# Change entire line to uppercase
gUU

# Change entire line to lowercase
guu

# Change all text to lowercase (from start to end)
:%s/.*/L&/

# Change all text to uppercase
:%s/.*/U&/

Working with Lines

# Remove blank lines
:g/^$/d

# Remove trailing whitespace at end of each line
:%s/s+$//e

# Remove leading whitespace at beginning of each line
:%s/^s+//e

# Remove all whitespace
:%s/s//g

# Join current line with next line
J

# Sort lines alphabetically
:%sort

# Sort and remove duplicate lines
:%sort u

# Reverse line order
:g/^/m0

Working with Multiple Lines

# Delete lines 5 through 10
:5,10d

# Copy lines 5 through 10 to line 20
:5,10co20

# Move lines 5 through 10 to line 20
:5,10m20

# Indent multiple lines (in Visual mode)
# Select lines with V, then press >

# Unindent multiple lines
# Select lines with V, then press <

Visual Mode Operations

Visual mode allows you to select text before performing operations:

# Character-wise visual mode
v

# Line-wise visual mode
V

# Block visual mode (rectangular selection)
Ctrl+v

Once text is selected:

  • d – Delete selection
  • y – Yank (copy) selection
  • c – Change selection (delete and enter insert mode)
  • > – Indent selection
  • < – Unindent selection
  • ~ – Toggle case
  • u – Convert to lowercase
  • U – Convert to uppercase

Working with Multiple Files

Buffers

# List all open buffers
:ls

# Switch to next buffer
:bn

# Switch to previous buffer
:bp

# Switch to buffer number 3
:b3

# Delete current buffer
:bd

# Open file in new buffer
:e filename.txt

Windows (Split Screen)

# Split window horizontally
:split or Ctrl+w s

# Split window vertically
:vsplit or Ctrl+w v

# Navigate between windows
Ctrl+w h/j/k/l (left/down/up/right)

# Close current window
:close or Ctrl+w c

# Make current window the only window
:only or Ctrl+w o

# Resize window
Ctrl+w + (increase height)
Ctrl+w - (decrease height)
Ctrl+w > (increase width)
Ctrl+w < (decrease width)

Tabs

# Open new tab
:tabnew filename.txt

# Switch to next tab
:tabn or gt

# Switch to previous tab
:tabp or gT

# Close current tab
:tabclose

# List all tabs
:tabs

Macros and Automation

Macros allow you to record a sequence of commands and replay them:

# Start recording macro in register 'a'
qa

# Perform your editing commands
# ...

# Stop recording
q

# Execute macro stored in register 'a'
@a

# Execute macro 10 times
10@a

# Execute last-used macro
@@

Advanced Vim Configuration

Customize Vim by editing the ~/.vimrc file:

" Enable line numbers
set number

" Enable relative line numbers
set relativenumber

" Enable syntax highlighting
syntax on

" Set tab width to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Enable auto-indentation
set autoindent
set smartindent

" Highlight search results
set hlsearch

" Enable incremental search
set incsearch

" Ignore case in searches
set ignorecase
set smartcase

" Show matching brackets
set showmatch

" Enable mouse support
set mouse=a

" Set color scheme
colorscheme desert

" Enable clipboard integration
set clipboard=unnamedplus

Practical Vim Use Cases

Editing Configuration Files

# Edit SSH config
sudo vim /etc/ssh/sshd_config

# Search for specific setting
/PermitRootLogin

# Make changes and save
:wq

# Restart service from within Vim
:!sudo systemctl restart sshd

Log File Analysis

# Open log file
vim /var/log/syslog

# Search for errors
/ERROR

# Filter and show only lines with ERROR
:g/ERROR/

# Copy all ERROR lines to new file
:g/ERROR/w >> errors.txt

# Count occurrences of ERROR
:%s/ERROR//gn

Programming with Vim

# Auto-indent entire file
gg=G

# Comment multiple lines (add # at beginning)
# Select lines in Visual mode (V)
:'s/^/# /

# Uncomment lines
:'s/^# //

# Format JSON
:%!python -m json.tool

# Format XML
:%!xmllint --format -

Troubleshooting and Tips

Common Issues

Accidentally entered Vim and can’t exit:

  • Press Esc to enter Normal mode
  • Type :q! and press Enter to quit without saving

Made changes but want to discard them:

  • :e! – Reload file, discarding all changes
  • :q! – Quit without saving

Vim is frozen:

  • You might have pressed Ctrl+s which freezes the terminal
  • Press Ctrl+q to unfreeze

Productivity Tips

  1. Learn incrementally – Don’t try to memorize all commands at once. Start with basics and gradually add more commands to your repertoire.
  2. Use the dot command – The . command repeats your last change. This is incredibly powerful for repetitive edits.
  3. Master text objects – Commands like ciw (change inner word), da" (delete around quotes) make editing much faster.
  4. Practice with vimtutor – Run vimtutor in your terminal for an interactive Vim tutorial.
  5. Disable arrow keys – Force yourself to use hjkl for navigation by adding this to your .vimrc:
    noremap  
    noremap  
    noremap  
    noremap  

Vim Plugins and Extensions

Extend Vim’s functionality with popular plugins:

  • vim-plug – Plugin manager for easy installation
  • NERDTree – File system explorer
  • fzf.vim – Fuzzy file finder
  • vim-airline – Status bar enhancement
  • coc.nvim – IntelliSense engine (code completion)
  • vim-fugitive – Git integration
  • syntastic – Syntax checking

Frequently Asked Questions

❓ What’s the difference between Vim and Vi?

Vim (Vi Improved) is an enhanced version of the original Vi editor. Vim includes many additional features like syntax highlighting, multiple undo levels, visual mode, command-line history, and extensive plugin support.

❓ Is Vim better than Nano or other text editors?

Vim has a steeper learning curve but offers far more power and efficiency once mastered. Nano is easier for beginners and simple edits. Vim excels at complex text manipulation, remote editing, and large file handling. The “better” choice depends on your needs and willingness to invest time learning.

❓ How long does it take to learn Vim?

Basic proficiency can be achieved in a few days using vimtutor. Comfortable daily usage typically takes 1-2 weeks of practice. Mastery is an ongoing journeyβ€”even experienced users continue discovering new techniques. Start with basics and gradually expand your command repertoire.

❓ Can I use Vim for programming?

Absolutely! Many professional developers use Vim as their primary editor. With plugins like coc.nvim for code completion, syntastic for syntax checking, and vim-fugitive for Git integration, Vim can become a full-featured IDE. It supports virtually every programming language through plugins.

❓ What is Neovim and should I use it instead?

Neovim is a modern fork of Vim with better plugin architecture, built-in LSP support, and async operations. For beginners, start with Vimβ€”it’s installed everywhere. Once comfortable, you can explore Neovim for its additional features. Most Vim commands work identically in both.

Conclusion

Vim is an incredibly powerful text editor that rewards the time invested in learning it. From basic text editing to complex file manipulation, Vim’s modal editing and extensive command set enable efficient workflows that are difficult to match with traditional editors.

The commands covered in this guide represent just the beginning of what’s possible with Vim. As you incorporate these commands into your daily workflow, you’ll discover that Vim becomes not just a text editor, but an extension of your thought processβ€”enabling you to edit text at the speed of thought.

Remember: start small, practice regularly, and gradually expand your command vocabulary. Before long, you’ll find yourself reaching for Vim instinctively whenever text editing is required.

Quick Reference Cheat Sheet

Essential Commands to Remember:

  • i – Enter insert mode
  • Esc – Return to normal mode
  • :wq – Save and quit
  • :q! – Quit without saving
  • dd – Delete line
  • yy – Copy line
  • p – Paste
  • u – Undo
  • /pattern – Search
  • :%s/old/new/g – Replace all

Happy Vimming! 🚀

Was this article helpful?

RS

About the Author: Ramesh Sundararamaiah

Red Hat Certified Architect

Ramesh is a Red Hat Certified Architect with extensive experience in enterprise Linux environments. He specializes in system administration, DevOps automation, and cloud infrastructure. Ramesh has helped organizations implement robust Linux solutions and optimize their IT operations for performance and reliability.

Expertise: Red Hat Enterprise Linux, CentOS, Ubuntu, Docker, Ansible, System Administration, DevOps

Add Comment