Fundamentals 10 min read

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

This article explains the differences between editors, compilers, and debuggers, then provides a comprehensive guide to Vim—including its modes, essential commands for insertion, navigation, deletion, copying, searching, saving, and quitting—along with practical tips, shortcut mappings, and configuration examples to enhance your coding workflow.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Master Vim: Essential Commands, Tips, and Configuration for Efficient Coding

First clarify the concepts of editor, compiler, and 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 VS to write C code, all of 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 on Linux/UNIX, used to create, edit, and display text files. It has no menus, only commands.

Modes

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

Insert mode.

Low line mode.

Summary: any mode switch can be done by pressing Esc to return to command mode.

1.2 Common commands in command mode

Insertion commands

Lowercase aio: a inserts after the cursor, i inserts before the cursor, o inserts a new line below. Uppercase AIO: A appends at the end of the line, I inserts at the line start, O inserts a new line above.

Navigation commands

Shift+ enters low line mode, then set nu shows line numbers, set nonu hides them.

GG jumps to the beginning of the last line, gg to the beginning of the first line.

nG or :n jumps to line n.

$ moves to the end of the current line, 0 moves to the start.

b searches backward, w forward, skipping one word each time.
hjkl correspond to left, down, up, right movement; arrow keys work as well.
L moves to the last line of the screen.

Deletion commands

x deletes the character under the cursor.

nx deletes n characters after the cursor; nX deletes n characters before.

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 the specified range of lines.

u undoes the last change.

Shift+~ toggles case of the selected text.

Copy and paste

yy copies the current line; nyy copies n lines below. p pastes after the cursor, P before.
dd cuts the current line; ndd cuts n lines below. p and P paste the cut text.

Search and replace

:%s/old/new replaces all occurrences in the file.

:n1,n2s/old/new/g replaces in the specified range.

r replaces a single character.

R replaces characters until ESC is pressed.

u cancels the last operation.

Save and quit

:w! saves changes.

:q! quits without saving.

:wq! saves and quits (or x!).

:w filename writes the buffer to another file.

Vim tricks

Import command output

:r! command inserts the output of a shell command at the cursor position.

Comment multiple lines

:n1,n2s/^/#/g adds a # at the start of lines n1‑n2.
:n1,n2s/^#//g removes a leading # from lines n1‑n2.

Define shortcuts

:map ^P I# defines a shortcut that inserts # at the beginning of a line.
:map ^B 0x defines a shortcut that deletes the first character of a line.
:ab mymail [email protected] defines an abbreviation for an email address.
Shortcuts are temporary; to make them permanent, add them to .vimrc (or /root/.vimrc for root users, /home/username/.vimrc for regular users).

Simple vim configuration (key points)

Basic configuration is written into .vimrc . Example installation command:
curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.sh
After installation, run source ~/.bashrc to apply the settings. Uninstall with bash ~/.VimForCpp/uninstall.sh . For a full configuration, refer to the GitHub repository VimForCpp .
VimshortcutsEditor
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

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.