Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
DevOps

Popular Vim Commands – Comprehensive Vim Cheat Sheet

Introduction

Vim is the most versatile text editor accessible to developers. Known for its efficiency and customizability, it relies heavily on a keyboard-centric approach, allowing users to navigate, modify, and manipulate text with considerable speed. Unlike traditional text editors, Vim offers multiple modes and a rich set of commands that empower users to perform complex tasks effortlessly.

For beginners, Vim’s steep learning curve and extensive feature set can seem intimidating. However, this challenge also underscores its potential. Once you grasp the basics and practice regularly, you can open up a new sort of productivity in text editing. This cheat sheet serves as your quick guide, breaking down essential Vim commands. Whether you’re managing configuration files, coding, or editing large documents, this resource will help you work faster, smarter, and with more precision.

Vim command

Vim command

Vim is unique because it operates through distinct modes, each tailored for specific tasks. Unlike traditional text editors where typing and navigating happen in one mode, Vim’s mode-based system enhances efficiency by separating functionalities. Following is the breakdown of the core Vim modes:

  • Normal Mode: When you open Vim, you get the normal mode as default mode. It’s used for navigating text and executing simple commands like deleting lines, copying text, or jumping between words or lines. You cannot type text directly in this mode.
  • Insert Mode: Switch to this mode when you need to type or modify text. You can enter it by pressing i for inserting at the cursor or a for appending after the cursor. Once you’re done, press Esc to come back to Normal Mode.
  • Visual Mode: This mode permits you to choose text for editing. You can highlight characters, words, or entire lines using v, V, or Ctrl-v for block selection. After selecting, commands like delete, copy, or replace can be applied to the highlighted text.
  • Command-Line Mode: Used to perform file operations and execute advanced commands. By pressing :, you can enter commands like :w to save, :q to quit, or :wq to save and exit. You can also search for text or perform substitutions from this mode.

Switching between these modes is essential to make the most of Vim’s capabilities. For example, you might navigate in Normal Mode, enter text in Insert Mode, and then save changes using Command-Line Mode—all seamlessly within the same session.

Moving inside the file

Moving inside the file

Navigating through your file efficiently is crucial for a smooth editing experience in Vim. With its extensive movement commands, you can quickly jump to specific locations within your file without ever reaching for the mouse. Here are the most prevalent employed commands for file navigation:

  • h, j, k, l: These are the basic directional keys to go left, down, up, and right, accordingly. They’re the foundation of Vim’s fast navigation.
  • 0: Places the cursor to the start of the current line.
  • ^: Takes you to the first non-blank character on the current line.
  • $: Jumps to the end of the line, allowing you to quickly navigate to the line’s conclusion.
  • w, e, b: These commands help you navigate by words. w places to the start of the next word, e takes you to the end of the current word, and b brings you to the start of the previous word.
  • gg: Quickly jumps to the very beginning of the file, saving time when navigating long documents.
  • G: Takes you straight to the end of the file, letting you skip scrolling to find the last line.

These commands make navigation faster and more efficient than using the arrow keys or a mouse.

Inserting text

Inserting text

Vim’s Insert Mode is where you can append or alter text. To transition into this mode, you need to know a variety of commands. Each command offers flexibility in how and where text is inserted, helping you customize your workflow:

  • i: Switches to Insert Mode, letting you begin typing at the cursor’s current position.
  • I: Places the cursor to the start of the current line and enters Insert Mode, permitting you to start typing from the start of the line.
  • a: Puts you in Insert Mode just after the cursor, enabling you to add text immediately after the current character.
  • A: Places the cursor to the line’s end, then allows you to add text.
  • o: Opens a new line below the current one & enters Insert Mode, perfect for adding new lines of text.
  • O: Similar to o, but opens a new line above the current one.

Once you’ve finished editing, pressing Esc returns you to Normal Mode. 

Editing text

Editing text

Vim’s editing commands offer a robust set of tools for manipulating text quickly and efficiently. Whether you’re deleting, changing, or replacing text, Vim lets you perform these actions with just a few keystrokes. Here’s how to use the most essential editing commands:

  • x: Eliminates the character under the cursor, useful for quick corrections.
  • dd: Removes the entire line where the cursor is positioned, perfect for deleting unnecessary lines.
  • dw: Discards from the cursor’s position to the current word’s end.
  • d$: Discards from the cursor to the current line’s end, saving you time compared to manually deleting each word.
  • u: Undoes the last change, restoring the file to its previous state.
  • Ctrl-r: Redoes the most recent undo, letting you quickly revert your actions.
  • r: Replaces the character under the cursor with a new one.
  • cw: Changes the current word, allowing you to replace it with something else.

These commands simplify text editing and make it possible to perform common tasks in just a few keystrokes. 

Saving and existing file

Saving and existing file

When working in Vim, knowing how to save your progress or exit the editor is crucial. These actions are performed in Command-Line Mode. Each command allows you to handle your file in different ways:

  • :w: This command saves the current file, guaranteeing that your modifications are kept.
  • :q: If no changes were made, this will exit Vim. However, if you have done modifications, Vim will warn you.
  • :wq or ZZ: These two commands save the file and then exit Vim in one step, making them efficient for saving and closing.
  • :q!: This forces Vim to quit without saving any modifcations, useful if you want to discard your edits.

These commands ensure that you either preserve your work or exit without saving, depending on your needs. 

Also Read: How to Copy, Cut, and Paste in Vim / Vi?

Enables Vim colors theme

Enables Vim colors theme

Customizing Vim with color themes can significantly improve your editing experience by enhancing readability and reducing eye strain. To enable a color theme in Vim, follow these steps:

  1. Open your Vim configuration file by typing vim ~/.vimrc in your terminal. This file controls your Vim settings.
  2. To apply a theme, add the line colorscheme [theme_name], replacing [theme_name] with your desired theme, such as colorscheme desert.
  3. Save the file by pressing :w and then restart Vim to apply the new theme.

Vim comes with various pre-installed color themes, and you can also explore more online. Some popular themes, like “desert,” “elflord,” and “morning,” are designed to improve visibility for different lighting conditions. 

Simple vim workflow example

Simple vim workflow example

Here’s a step-by-step guide to efficiently edit a file using Vim, focusing on the most common actions:

  1. Opening a file: Start by opening the file you want to edit. Use the command vim filename, where “filename” is your file’s name.
  2. Navigating through the file: Use commands like gg to jump to the top of the file or w to move by word. Alternatively, search for specific text by typing /search_term.
  3. Entering Insert Mode: Press i to enter Insert Mode and begin typing or modifying the text at the current cursor position.
  4. Exiting Insert Mode: Press Esc to come back to Normal Mode, where you can execute additional commands.
  5. Editing text: If you need to delete lines, use dd. To switch a character, press r followed by the character you desire to switch it with.
  6. Saving your modifications: As soon as you are satisfied with your edits, type :w to save your work.
  7. Exiting Vim: To close the file, use :q. If you desire to save & exit parallely, use :wq.

Also Read: How to find a Word in Vim or Vi Text Editor?

Conclusion

Vim is a resilient tool designed to optimize your workflow. Its wide range of commands and modes can transform the way you edit and manage files. Although the learning curve may appear steep, regular use makes these commands feel intuitive over time.

By practicing a few key commands daily and referring to this cheat sheet, you can steadily improve your proficiency. Vim’s versatility, speed, and efficiency make it invaluable for developers, system administrators, and power users at the same level. Whether you’re new to Vim or refining your skills, this guide provides a solid foundation to enhance your editing capabilities. Keep exploring, keep practicing, and unlock the true potential of Vim. Happy editing!

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles