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
ifrom 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)
- Position cursor at the start line
- Enter visual block mode:
Ctrl + v
- Select lines using
jork - Shift to insert mode at line start:
I
- Type the comment marker:
//
- Press
Esctwice to apply
Removing Comments
- Enter visual block mode:
Ctrl + v
- Select the comment characters
- 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:
- Press
ito enter Insert mode and write content - Press
Escto return to Normal mode - Use
yyandpto duplicate lines - Use
/patternto search for text - Type
:wqto 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.