Shell, system, and network tools

A short guide to vim

A short, beginner-friendly guide to the vim editor: how to open files, switch between command and insert mode, move around, edit text, and save and quit.

vim is a very popular file editor, especially among programmers. It’s actively developed, and there’s a very big community around it. There’s even a Vim conference!

On modern systems vi is just an alias to vim, which stands for Vi IMproved.

You start it by running vi:

Vim startup screen showing VIM - Vi IMproved version 8.0.1283 with help commands and exit instructions

You can pass a filename to edit that specific file:

vi test.txt

Vim editor opened with test.txt file showing empty document and New File status at bottom

You have to know that Vim has 2 main modes:

  • command (or normal) mode
  • insert mode

When you start the editor, you are in command mode. You can’t type text like you expect from a GUI editor, because every key is a command. To type, you have to enter insert mode. Press the i key. The -- INSERT -- label appears at the bottom of the editor:

Vim editor in insert mode showing INSERT indicator at the bottom of the screen

Now you can start typing the file contents:

Vim editor with sample text typed in insert mode showing Hey This is a very cool editor Its called Vim

You can move around the file with the arrow keys, or with the h j k l keys: h l for left and right, j k for down and up.

Once you are done editing, press esc to exit insert mode and go back to command mode:

Vim editor back in command mode with text displayed but no INSERT indicator at bottom

At this point you can navigate the file, but you can’t add content to it. Be careful which keys you press, they might be commands.

Now you want to save the file. Press : (colon), then w, then enter.

You can save and quit by pressing : then w and q: :wq

You can quit without saving by pressing : then q and !: :q!

That last one solves the famous “I’m stuck in Vim” problem. Press esc, type :q!, press enter, and you’re out.

You can undo an edit in command mode by pressing u, and redo (cancel an undo) by pressing ctrl-r.

Those are the basics of working with Vim. From here starts a rabbit hole we can’t go into in this little introduction. I will only mention the commands that will get you started:

  • pressing the x key deletes the character under the cursor
  • pressing A goes to the end of the current line and enters insert mode
  • press 0 to go to the start of the line
  • go to the first character of a word and press d followed by w to delete that word. If you follow it with e instead of w, the white space before the next word is preserved
  • use a number between d and w to delete more than 1 word, for example use d3w to delete 3 words forward
  • press d followed by d to delete a whole line. Press d followed by $ to delete from where the cursor is until the end of the line

To find out more about Vim I can recommend the Vim FAQ and especially running the vimtutor command. It should already be installed on your system, and it’s the best way I know to start your vim explorations.

Lesson completed