Master Vim: Essential Commands, Tips, and Configuration for Efficient Coding
This guide explains the differences between editors, compilers, and debuggers, introduces Vim's multi‑mode editing, provides essential command‑line operations, navigation, editing, searching, and saving shortcuts, and shows how to configure Vim with .vimrc for a smoother development experience.
First clarify the difference between an editor , a compiler and a debugger .
vim: an editor that only writes code, similar to Windows Notepad.
gcc: a compiler that only translates programs.
gdb: a debugger that only debugs programs.
When using Visual Studio to write C code, all these functions are integrated into the VS compiler because VS is an integrated development environment.
1. Vim usage learning
1.1 Vim introduction
Vim is a powerful multi‑mode editor, the most common text editor on Linux/UNIX, used to create, edit, and display text files. Vim has no menus, only commands.
Command mode (default): any input is treated as a command.
Insert mode.
Low‑line mode.
Summary: any mode can be switched back to command mode by pressing Esc .
1.2 Common commands in command mode
🥅 Insert commands
For lowercase aio : a inserts after the cursor, i inserts before the cursor, o inserts on a new line below. For uppercase AIO : A appends at the end of the current line, I inserts at the beginning of the line, O inserts on a new line above.
🥅 Navigation commands
Press Shift + : enter low‑line mode, then set nu to show line numbers, set nonu to hide them. GG jumps to the beginning of the last line; gg jumps to the beginning of the first line.
Jump to a specific line with nG or :n. $ moves to the end of the current line; 0 moves to the beginning.
Supplement: b searches backward, w searches forward, each skipping one word.
Supplement: hjkl correspond to left, down, up, right movements; arrow keys work as well.
Supplement: L moves to the last line of the screen.
🥅 Delete commands
xdeletes the character under the cursor. nx deletes n characters after the cursor; nX deletes n characters before the cursor. dd deletes the current line. D deletes from the cursor to the end of the line. ndd deletes n lines starting from the current line. dG deletes from the cursor to the end of the file. :n1,n2d deletes a specified range of lines. u undoes the last operation (similar to Ctrl+Z).
Supplement: Shift + ~ toggles case.
🥅 Copy and paste
yy copies the current line; nyy copies n lines downward. p pastes after the cursor, P before.
dd cuts the current line; ndd cuts n lines downward. p and P paste similarly.
🥅 Search and replace commands
:%s/old/newreplaces old with new in the whole file. :n1,n2s/old/new/g replaces in a range; g replaces without prompting, c prompts. r replaces the character under the cursor. R replaces characters from the cursor to the end of the line. u cancels the previous operation.
🥅 Save and quit commands
:w!save changes. :q! quit without saving. :wq! or x! save and quit (also Esc + Shift + ZZ). :w filename writes the buffer to filename (useful for backup).
2. Vim usage tips
2.1 Simple tips
🥅 Import command output to file: :r !command
Example: instead of leaving Vim, running date in the terminal, copying the result, and pasting it back, you can simply execute :r!date to insert the current time at the cursor.
🥅 Comment multiple lines
:n1,n2s/^/#/gadds # at the start of each line from n1 to n2.
Explanation: ^ moves to the beginning of a line, # is the comment character, g applies globally without prompting.
:n1,n2s/^#//gremoves the leading # from the specified range.
Explanation: replaces the leading # with nothing.
2.2 Define shortcuts
🥅 Define shortcut with :map
Example: :map ^P I# maps Ctrl+P to insert # at the beginning of the line.
:map ^B 0xmaps Ctrl+B to move to the start of the line and delete the first character.
🥅 Replace shortcut with :ab
Example: :ab mymail [email protected] lets you type mymail and press Enter to expand to the full email address.
Note: These shortcuts are temporary; to make them permanent, add them to .vimrc .
For the root user, write them to /root/.vimrc ; for normal users, write to /home/username/.vimrc .
3. Simple Vim configuration (key points)
All basic configurations are written into .vimrc. For a quick setup you can use an existing configuration:
curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.shAfter downloading, run source ~/.bashrc to apply the configuration.
To uninstall, run bash ~/.VimForCpp/uninstall.sh .
Recommended automatic configuration: https://github.com/askunix/VimForCpp . After installation, Vim feels as smooth as an IDE.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
