Fundamentals 11 min read

Master Vim: Essential Commands, Tips, and Configurations for Efficient Coding

This guide clarifies the differences between editors, compilers, and debuggers, then dives into Vim’s core concepts—modes, common commands for insertion, navigation, deletion, copying, searching, and substitution—plus advanced tricks, shortcut mappings, and essential .vimrc configurations to boost productivity.

Open Source Linux
Open Source Linux
Open Source Linux
Master Vim: Essential Commands, Tips, and Configurations for Efficient Coding

Understanding Editors, Compilers, and Debuggers

First, clarify the concepts: an editor writes code, a compiler translates programs, and a debugger handles program debugging. In Visual Studio, these functions are integrated into its IDE.

Vim Overview

Vim is a powerful multi‑mode editor, the most common text editor on Linux/UNIX, used to create, edit, and display text files. It has no menus, only commands.

Vim Modes

Command mode (default): all typed characters are interpreted as commands.

Insert mode.

Low‑line mode.

Switch between modes by pressing Esc to return to command mode.

Common Commands in Command Mode

Insertion Commands

a

– insert after the cursor. i – insert before the cursor. o – open a new line below the cursor. A – insert at the end of the current line. I – insert at the beginning of the current line. O – open a new line above the current line.

Navigation Commands

Enter low‑line mode with Shift+: then set nu to show line numbers, set nonu to hide. GG – go to the start of the last line; gg – go to the start of the first line. nG or :n – jump to line n. $ – move to the end of the line; 0 – move to the beginning of the line. b – move backward a word; w – move forward a word. h j k l – left, down, up, right (or use arrow keys). L – move to the last line of the current screen.

Deletion Commands

x

– delete the character under the cursor. nx – delete n characters after the cursor; nX – delete n characters before. dd – delete the current line. D – delete from the cursor to the end of the line. ndd – delete n lines starting from the current line. dG – delete from the cursor to the end of the file. :n1,n2d – delete a specific range of lines. u – undo the last change (similar to Ctrl+Z).

Copy and Paste

yy

– yank (copy) the current line; nyy – yank n lines starting from the current line. p – paste after the cursor; P – paste before the cursor. dd – cut the current line; ndd – cut n lines.

Search and Replace

:%s/old/new

– replace all occurrences of old with new in the entire file. :n1,n2s/old/new/g – replace in a specific line range; g for global, c to confirm each change. r – replace the character under the cursor. R – enter replace mode to overwrite characters until Esc. u – undo the previous operation.

Save and Quit

:w!

– save changes. :q! – quit without saving. :wq! or x! – save and quit. :w filename – write the buffer to a new file (useful for backup).

Vim Tips and Tricks

Import Command Output into a File

Use :r! command to insert the output of a shell command directly at the cursor. Example: :r!date inserts the current date.

Multi‑Line Commenting

:n1,n2s/^#/g

Comments lines n1 to n2 by inserting # at the start. :n1,n2s/^#//g Removes the leading # from the specified range.

Defining Shortcut Keys

Map a key sequence to a command with :map. Example: :map ^P I# Press Ctrl+V then P to insert # at the beginning of the line. :map ^B 0x Maps Ctrl+V B to delete the first character of the line.

Abbreviations

Define a shortcut for frequently typed text: :ab mymail [email protected] Typing mymail followed by Space expands to the full email address.

Note: These mappings are temporary; to make them permanent, add them to .vimrc.

.vimrc Configuration

For root users, place configurations in /root/.vimrc; for regular users, use ~/.vimrc.

Simple Vim Configuration Example

Basic settings are added to .vimrc. Example installation script:

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

After installation, run: source ~/.bashrc To uninstall:

bash ~/.VimForCpp/uninstall.sh
Original source: https://blog.csdn.net/m0_61933976/article/details/124653524
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.

ConfigurationproductivityVimeditor
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.