Editing Files With Vim

Vim is a terminal text editor installed on every Linux system. It has existed for decades and will likely exist until the heat death of the universe. So, if you plan to use Linux at all in your life, it is worth getting to know Vim. Becoming a Vim ninja takes many, many hours of dedicated work. Here, you will learn the basics so that you can survive. Many Linux applications use the same keyboard shortcuts as Vim, so learning a bit about Vim can help you in other administrative tasks.

Learning Objectives

You should be able to:

  • Launch Vim
  • Change between "normal" mode and insert mode
    • escape for normal mode
    • i for insert mode
  • Add text
  • Save files
    • :w
  • Exit Vim (very much not obvious)
    • :q
    • :wq
    • :q!

Video Walkthrough

Use this video to follow along with the steps in this lab.

Vim Modes

Vim is different than many other text editors in that it has two distinct modes:

  1. Normal mode. In this mode, the keys you type on the keyboard navigate the document or run commands rather than adding text.
  2. Insert mode. In this mode, the keys you type add text to the document.

If you are in normal mode when you want to write text, Vim will look like it's going nuts.

Create a File in Vim

This will walk you through the absolute basics of using Vim.

  • Navigate to your home directory, create a new directory named vimfun, and navigate to that director.
cd ~
mkdir vimfun
cd vimfun
  • Launch the Vim application just by running vim.
vim
  • (Note: vim is vi improved. In some systems, you will need to run vi instead of vim.)
  • The Vim editor will take over the terminal window.

Vim interface

  • Type the text fleck. Notice that no text appears in the window. This is because vim defaults to opening in normal mode.
  • Type i to enter insert mode. Note that the insert mode will be indicated in the bottom left.

Insert mode entered

  • Type the text fleck again. This time, the text will actually appear.
  • In insert mode, you can use the arrow keys, enter key, etc. to add text.
  • Add some text. Below is an example.

Sample text in vim

  • To save files in Vim, you have to change the mode to normal mode.
  • Press the escape key to exit insert mode and enter normal mode.
  • To save the file:
    • Press shift+: to enter the colon character.
    • Enter w file.txt. The w command tells Vim to write the contents to a file.
    • Press enter. The file should now be saved.
  • Press i to enter insert mode again.
  • Add some more text.
  • Press escape to leave insert mode and enter normal mode.
  • Enter :w and press enter. Because you already gave the file a name, you do not need to enter it again. All changes should be saved.
  • While still in normal mode, enter :q and press enter to quit Vim.

Edit a File in Vim

You created file.txt in the last section. Here, you will edit this file.

  • Run the following command to open file.txt in Vim.
vim file.txt
  • You should see all of the file's content.
  • Type i to enter insert mode.
  • Add some text to the file.
  • Press escape to enter normal mode.
  • Type :wq to save the changes and quit. The w and q commands can be run together or separately.
  • Display the file contents with cat.
cat file.txt

Contents of file.txt with cat

Exit Vim Without Saving

There are times when you make changes to a file but do not want to save the changes. You may have accidentally changed the file, or maybe you thought better about the changes you were about to make.

  • Edit file.txt with Vim again.
vim file.txt
  • Type i to enter insert mode.
  • Change some text.
  • Press escape to enter normal mode.
  • Type :q! to quit vim. Notice that the w command was omitted because we do not want to write the changes to disk. The q command quits Vim. The exclamation point (!) tells Vim to ignore any unsaved changes.

Power of vim

At this point, you might think that the people who made Vim are either crazy or masochists. But these exercises only touch on the most basic Vim functionality. By separating normal mode and insert mode, Vim can do very powerful things to make editing text efficient.

There are many ways to learn more advanced Vim features. One fun resource is https://vim-adventures.com/.

Reflection

  • What text editors do you prefer?
  • Why would it be worth investing time in learning a text editor?

Key Terms

  • vim: A highly configurable and powerful text editor for Unix-like operating systems. Vim stands for "Vi IMproved" and is an enhanced version of the older vi editor. It supports various modes for different types of text manipulation, extensive customization, and a wide range of plugins.
  • vim Normal Mode: The default mode in vim where users can navigate the text, delete, copy, paste, and perform other text manipulation tasks. In Normal Mode, keystrokes are interpreted as commands rather than text input. Users can switch to Normal Mode by pressing the Esc key.
  • vim Insert Mode: A mode in vim where users can insert and edit text. In Insert Mode, keystrokes are interpreted as text input rather than commands. Users can enter Insert Mode from Normal Mode by pressing keys such as i (insert before the cursor), a (append after the cursor), or o (open a new line below the cursor).