Fundamentals 18 min read

Master Vim: Essential Commands and Tips for Efficient Text Editing

This guide introduces Vim, the powerful Vi‑Improved editor, covering its lightweight nature, installation across Linux, macOS and Windows, core modes, essential navigation shortcuts, text manipulation commands, search and replace syntax, and file operations, enabling developers to edit code swiftly and efficiently without leaving the keyboard.

Deepin Linux
Deepin Linux
Deepin Linux
Master Vim: Essential Commands and Tips for Efficient Text Editing

Vim, short for "Vi IMproved", is a powerful, lightweight text editor that has been popular among programmers since its release in 1991. It starts quickly, runs on low‑resource machines, and relies heavily on keyboard shortcuts, allowing users to edit efficiently without using the mouse.

1. Vim Basics

1.1 Installation and Startup

Before using Vim, install it on your system. Installation varies by operating system.

On Linux, most distributions include Vim; you can install or update it via the package manager.

sudo apt-get update
sudo apt-get install vim

On CentOS or RHEL: sudo yum install vim On macOS, Vim is pre‑installed but may not be the latest version. Use Homebrew to install or update: brew install vim On Windows, download the installer from the official Vim website, run it, and start Vim from the command prompt with vim.

To open a specific file directly, run:

vim test.txt

1.2 Three Modes: Command, Insert, and Last‑Line

Vim has three primary modes. Mastering them is key to effective use.

(1) Command Mode

Vim starts in Command Mode, where you can move the cursor, delete, copy, and paste without inserting text.

h: move left one character

j: move down one line

k: move up one line

l: move right one character

Additional shortcuts include gg (go to file start), G (go to file end), and :50 (jump to line 50).

Insert Mode – press i to insert text, a to append after the cursor, o to open a new line below. Press Esc to return to Command Mode.

Last‑Line Mode – press : to enter command‑line mode for actions such as :w (save), :q (quit), :q! (force quit), :wq (save and quit), and substitution commands.

2. Vim Cursor Movement

Efficient cursor movement is essential for fast editing.

2.1 Move by Word

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 current or next word

Numeric prefixes can be combined, e.g., 2w moves forward two words.

2.2 Move Between Paragraphs and Sentences

(: move to the start of the current sentence

): move to the end of the current sentence

{: move to the start of the previous paragraph

}: move to the start of the next paragraph

2.3 Jump Between Matching Brackets

Press % on a bracket, parenthesis, or brace to jump to its matching counterpart, which is useful for navigating code structures.

3. Vim Deletion, Copying, and Pasting

These operations are fundamental and highly efficient in Vim.

3.1 Deletion Operations

x: delete the character under the cursor

dd: delete the entire current line (e.g., 3dd deletes three lines)

dw: delete from the cursor to the end of the current word

d$: delete from the cursor to the end of the line

3.2 Copying and Pasting

yy: yank (copy) the current line (numeric prefixes work, e.g., 2yy)

yw: yank from the cursor to the end of the current word

p: paste after the cursor

P: paste before the cursor

Using these shortcuts keeps your hands on the keyboard and greatly speeds up text manipulation.

4. Vim Search and Replace

Search and replace are powerful features for quickly locating and modifying text.

4.1 Search

Press / in Command Mode, type the pattern, and press Enter. Use n for the next match and N for the previous one. Regular expressions such as /int.*;/ can match complex patterns.

4.2 Replace

Replacement is performed in Last‑Line Mode with the syntax: :[range]s/search/replace/[options] range – lines to affect (e.g., 1,10 or % for the whole file)

s – the substitute command

search – pattern to find

replace – replacement text

options – e.g., g for global, c for confirmation

Example: :%s/hello/world/g replaces all occurrences of "hello" with "world"; 5,10s/hello/world/g limits the replacement to lines 5‑10; add c to confirm each change.

Mastering search and replace dramatically improves editing efficiency.

5. Vim File Operations

Basic file handling includes creating, opening, saving, and closing files.

5.1 Creating and Opening Files

Run vim new_file.txt to create a new file; Vim will open an empty buffer. To open an existing file, use vim existing_file.txt. Multiple files can be opened simultaneously, and you can switch between them with :next and :prev.

5.2 Saving and Closing Files

Save changes with :w. To save under a new name, use :w new_name.txt. Exit Vim with :q. If there are unsaved changes, use :wq to save and quit, or :q! to quit without saving.

These file‑related commands are fundamental to everyday Vim usage and greatly enhance workflow efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingText EditingproductivityVim
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

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.