Vishal Tejwani
by Vishal Tejwani
4 min read

Categories

  • vim

Vim is a command-line based text editor that is primarily used for editing code. It is lightweight (and hence fast) and is designed to be used entirely with the keyboard. There is no need for a mouse. Thanks to this, vim can be used to edit text very efficiently.

That being said, vim has a steep learning curve. It is quite different from conventional text editors that modern computer users are accustomed to. Due to this, it takes time to learn and master. And sometimes, you just need to use vim to make some quick changes to a file. Through this post, I hope to help you learn enough about vim to do exactly that.

I assume you already have vim installed.

Let’s get started!

Opening vim

You use the vim command to open the editor. You can also pass an argument to specify which file to open.

To open a file in vim:

vim [filepath]

Examples:

vim server.js
vim app/urls.py
vim ~/Documents/todo-list.txt

Vim creates the file if it doesn’t exist. However, the new file is not created immediately. “Saving” the file actually creates it.

Vim modes

Here’s where things branch off from other common editors. Vim is a modal editor - which means that it can operate in different modes. Conventional text editors have only one mode. When they are opened, you can directly start typing text into a file.

Vim is different. It has several modes. Depending on the mode, vim responds to keystrokes differently. You can switch between these modes as needed. The most common modes are normal mode and insert mode.

Vim is in normal mode by default when opened.

  • To move into insert mode, press the i key.
  • To move back into normal mode, press the Escape key.

Normal mode

Think of normal mode like a “command mode”. This mode is not for typing text, but for running editor commands. This is the number-one thing that trips beginners up. When in this mode, you can’t insert or delete text like in conventional editors. Rather, typing different keys runs different commands in the editor. For instance, saving changes, copying/cutting/pasting text, undo/redo, closing the editor, etc. It’s like having keyboard shortcuts for everything, and no mouse or menus.

Insert mode

As the name suggests, this mode is used to insert text. In this mode, typing actually inserts text. Heh. Pretty straightforward, right?

The key point to note is that this mode is only for inserting text. However, if you need to copy/cut/paste or run any other editor commands, you have to shift back to normal mode (by pressing Escape). To start typing again, you have to jump back into insert mode (by pressing i).

Vim in insert mode When in insert mode, an indicator is shown on the bottom left of the screen

Editing a file

After opening a file in vim, move into insert mode and edit the file as needed.

Then, to save the changes, move back into normal mode, and run the following command: :w (type :, then w, and then press Enter / Return to run the command). The w stands for write.

The command is visible on the bottom left of the screen as it is typed out.

The save command in vim The save command - :w

Exiting vim

Based on what you want to do, there are slightly different commands to exit vim. First, switch to normal mode since we need to run a command. Then:

To exit, run: :q (The q stands for quit). This command fails if there are any unsaved changes in the file.

The quit command in vim The quit command - :q

To save the changes and quit vim in a single command, you can run: :wq (write and quit).

The vim command to save and quit The command to save and quit - :wq

To forcefully exit without saving, run: :q!

The vim command to quit without saving The command to quit without saving - :q!

Summary

  • To open vim: vim [filepath]
  • Vim is a modal editor. The two most common modes are normal mode and insert mode.
  • To move into insert mode, press the i key. In insert mode, you can add/edit text by typing.
  • To move back into normal mode, press the Escape key. In normal mode, keystrokes are treated as commands. The characters for the keystrokes are not inserted into the file.
  • To save changes to a file, move into normal mode and run the command: :w. Hit Enter / Return to run the command after typing it in.
  • To exit vim, move into normal mode and run one of the following commands:
    • Quit: :q (fails if there are any unsaved changes)
    • Save and quit: :wq
    • Quit without saving: :q!

Learn vim

In this post, I skimmed over using vim for basic text editing. In doing so, I explained some vim concepts in a superficial manner.

As mentioned earlier, vim can be used to edit text very efficiently. In my opinion, knowing how to use vim well is a great skill to have for programmers. Consider investing some time into learning vim in more depth. 😄

There are lots of great resources available to learn vim. Here are some of my favourites:

  • vimtutor - Run the vimtutor command in your command-line.
  • VIM Adventures - This is a browser-based game designed to be a vim tutorial.