A Detailed Guide to Vim Commands

Md.Aminul Islam Sarker
4 min readMay 29, 2023

--

Introduction

Vim, short for “Vi IMproved,” is an advanced text editor that allows for efficient text manipulation. It’s based on the vi editor written by Bill Joy for the Unix operating system. Due to its ability to help users edit text files quickly, Vim is very popular among system administrators, data scientists, and developers.

This article aims to provide a detailed guide to Vim commands, helping both beginners and intermediate users harness the full power of this incredible tool.

Basic Vim Commands

Let’s start with the basics. These are commands that are useful for navigation and simple editing tasks.

  • vim filename: Opens a file in Vim. If the file doesn't exist, Vim will create it.
  • :q: Quits Vim. If there are unsaved changes, Vim will warn you.
  • :q!: Quits without saving changes.
  • :w: Saves changes.
  • :wq or :x: Saves changes and quits Vim.
  • :e filename: Opens another file without closing Vim.
  • dd: Deletes the current line.
  • yy: Yanks (copies) the current line.
  • p: Pastes the yanked text after the current line.
  • u: Undoes the last operation.
  • Ctrl + r: Redoes the last operation that was undone.
  • :set number: Shows line numbers.
  • :set nonumber: Hides line numbers.
  • h, j, k, l: Navigates the cursor to the left, down, up, and right, respectively.

Command Mode Commands

Command mode is where Vim shines. You can execute complex commands that can edit, delete, or copy multiple lines at once. Here are some of the most used command mode commands:

  • :s/old/new: Replaces the first occurrence of 'old' with 'new' in the current line.
  • :s/old/new/g: Replaces all occurrences of 'old' with 'new' in the current line.
  • :%s/old/new/g: Replaces all occurrences of 'old' with 'new' in all lines.
  • :10,20d: Deletes lines from 10 to 20.
  • :10,20y: Yanks (copies) lines from 10 to 20.
  • :10,20m30: Moves lines from 10 to 20 to after line 30.
  • :10,20co30: Copies lines from 10 to 20 and pastes them after line 30.

Insert Mode Commands

Vim has several commands to enter insert mode, each one allowing you to start typing text in different places:

  • i: Enters insert mode before the cursor.
  • I: Enters insert mode at the beginning of the line.
  • a: Enters insert mode after the cursor.
  • A: Enters insert mode at the end of the line.
  • o: Opens a new line below the current line and enters insert mode.
  • O: Opens a new line above the current line and enters insert mode.

To exit insert mode and return to command mode, press ESC.

Visual Mode Commands

Visual mode is useful for selecting blocks of text. You can enter visual mode by pressing v in command mode. After that, you can move the cursor to select text:

  • v: Enters visual mode.
  • V: Enters visual line mode, selecting whole lines.
  • Ctrl + v: Enters visual block mode, allowing you to select a rectangular block of text.
  • y: Yanks (copies) the selected text.
  • d: Deletes the selected text.

Search Commands

Vim’s powerful search commands are another reason for its popularity. Here are some commands that you can use to search text:

  • /pattern: Searches for 'pattern' forward in the file.
  • ?pattern: Searches for 'pattern' backward in the file.
  • n: Repeats the last / or ? command.
  • N: Repeats the last / or ? command in the opposite direction.
  • :%s/pattern/replacement/g: Replaces every occurrence of 'pattern' with 'replacement' in the entire file.

Conclusion

The power of Vim lies in its ability to help you manipulate text quickly and efficiently. This list of commands barely scratches the surface of what Vim can do. As you get more comfortable with these basic commands, you can start learning about more complex features, like recording macros, using buffers and windows, and even scripting Vim to automate common tasks.

Remember that learning Vim is like learning a musical instrument: it takes time and practice. Stick with it, and you’ll find that your speed and efficiency in text manipulation will greatly improve. Happy Vim-ing!

--

--

Md.Aminul Islam Sarker
Md.Aminul Islam Sarker

Written by Md.Aminul Islam Sarker

Seasoned IT pro with a passion for web dev, software engineering & project management. Skilled in Rust, PHP, JS, Java & AWS. Let's explore tech together!

No responses yet