Vim Editor Essential Commands With PDF Cheat Sheet

Vim Editor, short for Vi Improved, is a highly configurable text editor used to create and efficiently edit text. It offers a plethora of features for customizing the Vim editor to suit your own environment. Vim editor supports different modes, each designed for specific tasks, providing basic to advanced text and code editing capabilities.

In this blog post, I will discuss the essential commands to help you get started with Vim Editor and boost your productivity. Additionally, I will provide a downloadable cheat sheet for future reference.

Vim Modes and Navigation

Vim editor has three distinct modes of operation: Normal Mode, Insert Mode, and Visual Mode. Each mode has specific capabilities and workflows. Understanding the modes of Vim editor is essential before you start using it.

Normal Mode

When you first open a document with Vim editor, it opens in the default mode called Normal mode. This mode is primarily used for navigation and quick editing of any document or configuration files. Normal mode is super efficient for reading and modifying documents with Vim editor. It supports several key bindings to maximize your workflow and productivity. Here is a reference table of common navigational keybindings in Normal mode:

Key CommandAction
hMove the cursor left
jMove the cursor down
kMove the cursor up
lMove the cursor right
ggGo to the beginning of the file
GGo to the end of the file
0Go to the beginning of the current line
$Go to the end of the current line
Ctrl+fMove forward one screen
Ctrl+bMove backward one screen

Insert Mode

Another mode is Insert mode. Press the i key to enter Insert mode. In Insert mode, you can interact with your document like a normal text and code editor. You can write and change code, use arrow keys for navigation, and backspace or delete keys for deleting characters and lines within Insert mode. Use the Esc key to exit Insert mode and return to Normal mode.

Normal and Insert mode of vim editor

Visual Mode

Another mode in Vim editor is Visual mode. This mode allows you to select multiple blocks of text with your cursor. Press the v key to enter Visual mode. You can also navigate within Visual mode using commands similar to Normal mode. Here are the basic navigational commands in Visual mode:

Key CommandAction
hMove the cursor left
jMove the cursor down
kMove the cursor up
lMove the cursor right
wSelect a word
ggSelect from the current line to the beginning of the file
GSelect from the current line to the end of the file
Ctrl+vEnter Visual Block mode and select text in a rectangular block

Vim Essential Commands

There are numerous commands and shortcuts you can use in Vim editor to boost your productivity. These shortcuts and keybindings allow you to quickly work with text files in the command-line environment.

Saving Files

To save your changes in Vim, use the following commands:

Key CommandAction
:wWrite the current file to disk without quitting Vim. Saves any modifications you have made.
:w new_filenameSave the current file with a new name, creating a new file with the specified new_filename.
:wqWrite the file and quit Vim. Saves the changes and exits the editor
ZZSave and quit. If changes have been made, this command writes the file and quits Vim. If no changes have been made, it simply exits.

Quitting Vim

To exit Vim without saving changes, you can use the following commands:

Key CommandAction
:qQuit Vim. Exits the editor if there are no unsaved changes. If changes have been made, Vim displays an error message and prevents you from quitting.
:q!Quit Vim without saving changes. Forcefully exits Vim, discarding any modifications you made.

Vim provides additional navigation commands to quickly move within files:

Key CommandAction
Ctrl+uScroll half a screen up
Ctrl+dScroll half a screen down
Ctrl+eScroll one line up
Ctrl+yScroll one line down
Ctrl+oJump to the previous location
Ctrl+iJump to the next location

Editing Text in Vim

Vim provides a number of editing commands to effectively edit your document and configuration.

Inserting and Appending Text

When you’re in Normal mode, you can switch to Insert mode to directly insert or append text. Here are the commands to enter Insert mode:

Key CommandAction
iStart inserting text at the cursor’s current position.
IStart inserting text at the beginning of the current line.
aStart appending text after the cursor.
AStart appending text at the end of the current line.
oOpen a new line below the current line and enter Insert mode.
OOpen a new line above the current line and enter Insert mode.

Once in Insert mode, you can freely type and edit text. To return to Normal mode, press the Esc key.

Deleting and Cutting Text

Vim provides commands to delete or cut (copy and delete) text. The deleted or cut text can be subsequently pasted elsewhere in the file. Here are the essential commands for deleting and cutting text:

Key CommandAction
xDelete the character under the cursor.
dwDelete the current word.
ddDelete the current line.
DDelete from the cursor position to the end of the line.
yyCopy the current line (yank).
pPaste the deleted or copied text below the cursor.
PPaste the deleted or copied text above the cursor.

Copying and Pasting Text

In addition to cutting and pasting, Vim allows you to copy and paste text without deleting it. Here are the essential commands for copying and pasting:

Key CommandAction
yyCopy the current line.
yiwCopy the current word.
y$Copy from the cursor position to the end of the line.
pPaste the copied text below the cursor.
PPaste the copied text above the cursor.

Undoing and Redoing Changes

Vim provides commands to undo and redo changes made to the text. These commands help you revert or reapply modifications as needed. Here are the essential commands for undoing and redoing changes:

Key CommandAction
uUndo the most recent change
Ctrl+rRedo the most recent change that was undone

Indenting and Formatting Text

Vim allows you to indent and format your text to improve readability and maintain consistent

styles. Here are the essential commands for indenting and formatting text:

Key CommandAction
>>Indent the current line
<<Unindent the current line
=Auto-indent the selected lines or current block of code
gg=GAuto-indent the entire file

Repeating Previous Commands

To repeat a previous command in Vim, you can use the . (dot) command. The dot command will repeat the last change or command you performed. For example:

Key CommandAction
ddDelete a line
.Delete the next line
yyCopy a line
.Paste the copied line below

The dot command is a powerful tool for repeating actions and maintaining a consistent editing flow.

Advanced Vim Command

The Vim editor not only supports basic commands but also offers a wide range of advanced functionalities. These include managing multiple files, viewing command line history, customizing split windows, and more.

Managing Multiple Files

Vim provides efficient ways to work with multiple files simultaneously. Here are some commands to help you manage multiple files:

Key CommandAction
:e filenameOpen a new file named filename in a new buffer.
:lsList all the open buffers and their respective file names.
:b buffer_numberSwitch to a specific buffer by specifying its buffer_number.
:bnGo to the next buffer.
:bpGo to the previous buffer.
:bd buffer_numberClose a specific buffer by specifying its buffer_number.

These commands allow you to switch between files, close buffers, and efficiently work with multiple documents within Vim.

Customization and Settings

Vim’s command-line interface allows you to customize various settings and options to tailor the editor to your needs. Here are some commands for customization:

Key CommandAction
:set numberEnable Line Number
:set nonumberDisable Line Number
:syntax onEnable syntax highlighting
:syntax offDisable syntax highlighting
:colorscheme scheme_nameSet a specific color scheme for the editor

These commands enable you to customize Vim’s behavior, appearance, and functionality according to your preferences.

Command-line History

Vim’s command-line history allows you to access and reuse previously entered commands. Here are some commands for managing the command-line history:

Key CommandAction
:historyDisplay the command-line history.
:up or Ctrl+pCycle through previously entered commands.
:down or Ctrl+nCycle through the command history in reverse order.

These commands make it convenient to access and reuse frequently used commands, saving you time and effort.

Split Windows

Vim allows you to split the editing window horizontally or vertically, enabling you to work on multiple files or different sections of the same file simultaneously. Here are the commands to split windows:

Key CommandAction
:splitSplit the window horizontally.
:vsplitSplit the window vertically.
Ctrl+w, wSwitch between windows.
Ctrl+w, _Maximize the current window vertically.
Ctrl+w,

Split windows provide a powerful way to multitask and improve productivity within Vim.

Abbreviations

Abbreviations in Vim allow you to define shortcuts for longer pieces of text. They can be particularly helpful for repetitive tasks or when working with complex code snippets. Here’s how to define an abbreviation:

  • :ab abbreviation replacement: Defines an abbreviation where abbreviation is the shortcut you want to use and replacement is the full text it will expand to.

For example, you could define an abbreviation like this:

  • :ab myname John Doe: Typing myname will automatically expand to John Doe.

Conclusion

Vim is one of the most powerful command line text editor. You can enhance your coding skills in Vim using the following commands within the Vim editor. In this blog post, I’ve covered the basics to advanced Vim commands to help you get started with Vim.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *