What is Vim?

Vim is a free and open-source, screen-based text editor program. It is an improved clone of Bill Joy's vi. Vim's author, Bram Moolenaar, derived Vim from a port of the Stevie editor for Amiga and released a version to the public in 1991. 

Understand the Vim modes

Before we start, let’s know about the Vim modes. Vim has three modes:

  • Command Mode: When you start Vim, you are placed in Command mode. In this mode, you can move across the screen, delete text and copy text.
  • Insert Mode: You cannot write text in command mode. To write text (or let’s say insert text) into a file, there is a dedicated insert mode. When you want to write something on a file, you must enter the insert mode.
  • Visual mode: Somewhat like command mode but here, you can use the arrow keys to select text across lines (instead of directly working on words and lines based on cursor position).

Once you know the Vim modes, let’s see some basic Vim commands for various purposes.

Basic Vim Commands Cheat Sheet

Vim CommandDescription
iEnter insert mode
EscEnter command mode
x or DelDelete a character
XDelete character is backspace mode
uUndo changes
Ctrl + rRedo changes
yyCopy a line
ddDelete a line
pPaste the content of the buffer
/<search_term>Search and then cycle through matches with n and N
[[ or ggMove to the beginning of a file
]] or GMove to the end of a file
:%s/foo/bar/gciSearch and replace all occurrences with confirmation
Esc + :wSave changes
Esc + :wq or Esc + ZZSave and quit Vim
Esc + :q!Force quit Vim discarding all changes

Entering insert mode in Vim

Always remember, i stands for insert mode. You press the ‘i’ key to enter the insert mode.

When you enter the insert mode, you’ll see — INSERT — at the bottom of the editor/terminal screen. This indicates that you are in insert mode.

But i is not the only way to enter the insert mode. There are several other commands to enter the insert mode in Vim. The cursor position is important while entering the insert mode.

  • i – New text will appear before the cursor
  • a – New text will appear after the cursor
  • I – New text will appear at the beginning of the current line
  • A – Next text will appear at the end of the current line
  • o – A new line is created after the current line
  • O – A new line is created before the current line


Going back to command mode in Vim

You start in command mode, you go in to insert mode. If you want to move back to the command mode, press Esc (escape) key.

When you have entered your text, I advise hitting Esc key to enter command mode. This way, you won’t enter any new text unknowingly, involuntarily.

Undo your changes in Vim

If you made a mistake in Vim, you don’t have to live with it forever. I have also seen people quitting the editor without saving in such cases which is sort of ridiculous.

Vim allows you to undo and redo your changes. You can also redo your changes. It’s all applicable in the same session, of course. Once you have saved your changes, you cannot undo it.

To undo a change, go to command mode and press ‘u’ key. You can press it several times for multiple undo actions.

If you want to redo a change, press Ctrl+r key combination in the command mode. You can press it several times for multiple redo actions.

Deleting in Vim

Apart from undoing your changes, you might also want to delete some text from the file. In Vim, you can always use the Delete key for deleting a character but there are a few key combinations for better handling the deletion in Vim.

  • x – Delete the character at the current cursor position just like a delete key
  • X – Delete the character before the current cursor position like a backspace key. Note that the backspace key itself doesn’t work in Vim.
  • dw – Delete word. Actually, it deletes from the current cursor position till the end of the current word plus the white space after it.
  • dd – Delete the current line.
  • d$ – Delete from the current cursor position till the end of the line.
  • dG – Delete from the current cursor position till the end of the file.

Copy-Pasting in Vim

You might wonder how to copy paste in Vim. This is a legitimate concern because you won’t always type in the texts.

There are two kinds of copy and paste in Vim. One that deals with the buffer and one that deals with the ‘external’ text.

When you are in command mode, you can either use your Linux terminal shortcuts for copying the text or the following key combinations for copying text:

  • yw – Copy word. Actually, it copies from the current cursor position till the end of the current word plus the white space after it.
  • yy – Copy current line.
  • y$ – Copy from the current cursor position till the end of the line.
  • yG – Copy from the current cursor position till the end of the file.

Suppose you used one of the delete commands discussed above. The deleted text is placed into the buffer. You can paste this text from the buffer using these two paste commands:

  • p – Paste the contents of the buffer before the cursor position
  • P – Paste the contents of the buffer after the cursor position

The paste commands only work with the Vim buffer. What about the text you copied from some other file? In those cases, you can use the standard copy and paste key combinations of your Linux Terminal.

In Ubuntu and many other Linux distribution’s default terminal, Ctrl+Shift+C is used for copying and Ctrl+Shift+V is used for pasting. In some other terminals, selecting a text copies it and you can paste it using the right click.

Whatever may be the way to copy paste in your Linux terminal, you can also use it in Vim. Just make sure that you are in the insert mode for this type of copy pasting.

Searching for text in Vim

Finding a particular text is an important function of a text editor. You can search for text in the file in Vim using ‘/’.

In the command mode, use / and then type your search text and press enter. You’ll see whatever you are typing in the bottom left of the screen.

It will do a forward searching for the searched term from your cursor position. You can also use ‘?’ and then type your search term and press enter to perform a backward search. Note that the search is case sensitive.

If there is more than one match for your search text, you can jump to the next position by pressing n key. If you want to go back to the previous match, press N. Basically, with n and N you can cycle through all the matches. Tip: You can use \c option to run a case-insensitive search in Vim. Example: /\cMy_Search

Search and Replace in Vim

Vim provides a substitute command (:s) for searching and replacing text. It relies heavily on regular expressions (regex) for search and replace.

You can use it in the following fashion:

:%s/foo/bar/g
The above command will replace all the ‘foo’ with ‘bar’ in the entire file. The ‘g’ at the end is responsible for extending the search and replace function on all the matches. Otherwise, only the first match is replaced.

:s/foo/bar/g
The ‘:s’ command will do the exact same function as above but only in the current line instead of the entire file.

Saving and quitting Vim

You have just learned the basics of Vim commands. It’s time to save your work and exit Vim. To save or exit Vim, you must enter the command mode first by press Esc key. And then you can use the following options:

  • :w – Save the changes but don’t exit
  • :wq – Save and quit
  • :q – Just Quit (if you have unsaved changes, you’ll see this warning: E37: No write since last change (add ! to override))
  • :q! – Force quit (it will discard any unsaved changes)