Beginner’s Guide to VI Shortcuts: Master Text Editing Basics

The VI editor is a powerful, efficient, ubiquitous text editor in the Unix/Linux ecosystem. For many developers and system administrators, it is a critical tool for editing configuration files, writing scripts, or coding. While VI may initially seem daunting due to its unique modal interface, its shortcodes make it an extremely productive tool when mastered.

This article will explore the essential VI shortcodes that can supercharge your editing efficiency.

What Are VI Short Codes?

Shortcodes in VI are key combinations or commands that perform specific actions. These codes allow you to navigate, edit, search, and manipulate text quickly and effectively. VI operates in different modes:

  • Command Mode: Default mode for navigation and issuing commands.
  • Insert Mode: For text input.
  • Visual Mode: For selecting and manipulating blocks of text.

Knowing which shortcodes work in each mode is key to using VI effectively.

vi editor

Navigating efficiently in VI is fundamental. Here are the basics:

Short CodeAction
hMove left by one character
lMove right by one character
jMove down by one line
kMove up by one line
wMove forward to the start of the next word
bMove backward to the start of the word
0Move to the beginning of the current line
^Move to the first non-whitespace character
$Move to the end of the current line
GGo to the end of the file
ggGo to the beginning of the file
Ctrl+dScroll down half a screen
Ctrl+uScroll up half a screen
:nJump to line n
:set nuTo get line numbers

Editing Short Codes

VI offers quick ways to insert, delete, and modify text:

Short CodeAction
iEnter insert mode before the cursor
IEnter insert mode at the beginning of the line
aEnter insert mode after the cursor
AEnter insert mode at the end of the line
oOpen a new line below the current line
OOpen a new line above the current line
xDelete the character under the cursor
ddDelete the current line
DDelete from the cursor to the end of the line
yyCopy (yank) the current line
pPaste after the cursor
PPaste before the cursor
uUndo the last action
Ctrl+rRedo the undone action
r<char>Replace the character under the cursor with <char>
cwChange the word under the cursor

Searching and Replacing

Searching and replacing text is straightforward with VI short codes:

Short CodeAction
/patternSearch forward for pattern
?patternSearch backward for pattern
nRepeat the last search in the same direction
NRepeat the last search in the opposite direction
:%s/old/new/gReplace all occurrences of old with new in the file
:n,m s/old/new/gSearch backwards for pattern

Working with Lines and Blocks

These shortcodes help manage and edit lines or blocks of text:

Short CodeAction
>>Indent the current line
<<Un-indent the current line
VEnter visual line mode
Ctrl+vEnter visual block mode
:nGo to line number n
:.,.+3dDelete the current line and the next three

Exiting VI

Exiting VI properly is crucial to avoid unintended changes or losing your work:

Short CodeAction
:wSave the file
:qQuit if no changes have been made
:wq or ZZSave and quit
:q!Quit without saving changes

Conclusion

Mastering VI shortcodes transforms the editor into a seamless extension of your workflow. Although the learning curve may seem steep initially, the efficiency gained in editing, navigating, and managing text makes VI an invaluable tool. Whether you are a developer, system administrator, or Linux enthusiast, investing time in learning VI will pay dividends in productivity.

Resources

Leave a Comment