Mastering Vim: A Complete Guide to Text Editing in Linux

Overview

Vim is a powerful modal text editor that comes pre-installed on most Linux distributions. Unlike traditional editors, Vim operates through different modes, each designed for specific tasks. This guide covers essential Vim operations including text navigation, editing, and configuration.

Understanding Vim Modes

Vim operates primarily in three modes:

  • Normal Mode: The default mode for navigating and manipulating text. Keystrokes here trigger commands rather than inserting characters.
  • Insert Mode: Entered by pressing i from Normal mode. Used for actual text input.
  • Command-Line Mode: Accessed by typing : from Normal mode. Used for file operations and settings.

Transitions between modes follow a specific pattern: both Insert and Command modes return to Normal mode via the Esc key. However, Insert and Command modes cannot switch directly to eachother.

Installation

On CentOS/RHEL-based systems, install Vim with:

sudo yum install -y vim

On Debian/Ubuntu systems, use:

sudo apt-get install -y vim

Basic File Operations

Create and open a file:

vim main.c

Cursor Movement

In Normal Mode

Navigation uses either arrow keys or the HJKL cluster:

Key Direction
h Left
l Right
j Down
k Up

Advanced Navigation

Jump to specific positions:

gg          " Move to file beginning
G           " Move to file end (uppercase)
5000gg      " Jump to line 5000
^           " Beginning of current line
$           " End of current line
w           " Next word
b           " Previous word

In Insert Mode

Only arrow keys work for cursor movement.

Text Manipulation

Copy Operations

yy          " Copy current line
3yy         " Copy 3 lines starting from cursor
p           " Paste after cursor
10000p      " Repeat paste operation 10000 times

Cut/Delete Operations

dd          " Delete current line
3dd         " Delete 3 lines starting from cursor
p           " Paste after deletion

Undo/Redo

u           " Undo last change
Ctrl + r    " Redo (restore what was undone)

Comment Operations

Adding Comments (Block Selection)

  1. Position cursor at the start line
  2. Enter visual block mode:
Ctrl + v
  1. Select lines using j or k
  2. Shift to insert mode at line start:
I
  1. Type the comment marker:
//
  1. Press Esc twice to apply

Removing Comments

  1. Enter visual block mode:
Ctrl + v
  1. Select the comment characters
  2. Delete:
d

Command-Line Mode Operations

Save and Exit

:w          " Save file
:q          " Quit (fails if unsaved)
:wq         " Save and quit
:q!         " Force quit without saving
:wq!        " Force save and quit

Execute Shell Commands

Run shell commands without leaving Vim:

:!touch backup.txt
:!ls -la

Multiple Windows

Open multiple files side by side:

:vs filename    " Vertical split
:sp filename    " Horizontal split
Ctrl + w        " Switch between windows

Automatic Configuration

The VimForCpp project provides an optimized configuration for C++ development. This installation works on CentOS 7 with non-root users:

curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.sh

Remove the configuration:

bash ~/.VimForCpp/uninstall.sh

Common Workflow Example

A typical editing session might proceed as follows:

vim config.json

Inside Vim:

  1. Press i to enter Insert mode and write content
  2. Press Esc to return to Normal mode
  3. Use yy and p to duplicate lines
  4. Use /pattern to search for text
  5. Type :wq to save and exit

Practice Recommendations

Mastering Vim requires consistent practice. Start with simple operations like navigation and basic editing, then gradually incorporate more advanced features. The modal approach, while initially unintuitive, significantly increases editing speed once familiarity develops.

Tags: Linux Vim Text Editor Command Line Terminal

Posted on Tue, 30 Jun 2026 17:38:08 +0000 by robk