DEV Community

Cover image for Vim: Commands Every Developer Must Know 💻🫵
Martins Gouveia
Martins Gouveia

Posted on

Vim: Commands Every Developer Must Know 💻🫵

No matter if you are a sysadmin or a software developer, if you work in the Linux terminal, you would face the situation where you need to edit text files in the terminal

What is Vim?

Vim is a powerful text editor used in Unix/Linux systems, known for its efficiency and flexibility. It operates in different modes, each serving a specific purpose. Here are some essential Vim commands to help you get started:

Modes in Vim

Vim has three primary modes:

Command Mode: This is the default mode when you start Vim. You can navigate, delete, and copy text in this mode.

Insert Mode: Used for inserting text into the file. Enter this mode by pressing i, a, I, A, o, or O.

Visual Mode: Allows you to select text using the arrow keys.

Basic Commands

Entering Insert Mode

  • i: Insert before the cursor.
  • a: Insert after the cursor.
  • I: Insert at the beginning of the line.
  • A: Insert at the end of the line.
  • o: Open a new line below the current line.
  • O: Open a new line above the current line.

Navigating in Vim

  • h: Move left.
  • j: Move down.
  • k: Move up.
  • l: Move right.
  • gg: Move to the beginning of the file.
  • G: Move to the end of the file.
  • nG: Move to line number n.

Editing Text

  • x: Delete the character under the cursor.
  • X: Delete the character before the cursor.
  • dd: Delete the current line.
  • dw: Delete from the cursor to the end of the word.
  • d$: Delete from the cursor to the end of the line.
  • dG: Delete from the cursor to the end of the file.

Copying and Pasting

  • yy: Copy the current line.
  • yw: Copy from the cursor to the end of the word.
  • y$: Copy from the cursor to the end of the line.
  • yG: Copy from the cursor to the end of the file.
  • p: Paste after the cursor.
  • P: Paste before the cursor.

Undo and Redo

  • u: Undo the last change.
  • Ctrl + r: Redo the undone change.

Searching and Replacing

  • /search_term: Search for search_term.
  • n: Move to the next match.
  • N: Move to the previous match.
  • :%s/foo/bar/g: Replace all occurrences of foo with bar in the entire file.
  • :%s/foo/bar/gc: Replace all occurrences with confirmation.

Saving and Quitting

  • :w: Save the file.
  • :wq: Save and quit.
  • :q: Quit (if no changes have been made).
  • :q!: Quit without saving changes.

These commands cover the basics of using Vim effectively. For more advanced usage, refer to the detailed guides and cheat sheets available online

Top comments (0)