Your Vim Tutorial: Create, Edit and Save Files

Vim is a common and powerful text editor available for many operating systems. Learn the basics of Vim by creating and editing a file.

What is Vim_ And Why Do You Need itThe vi editor (pronounced vee-eye) is a common tool on Unix-based systems. The modern version is vi-Improved or Vim (pronounced "vim"). This tutorial covers how to get started using this powerful and standard text editor.

For those new to command-line environments, Vim can be a bit terrifying. It's a text editor designed for non-graphical interfaces, where there are no convenient pull-down menus or mouse-driven events. Everything is done with the keyboard. Linux servers often do not have a graphical user interface (GUI) installed, something startling to Windows and macOS users. Another significant difference is that Linux stores its configurations in text files, so if a Linux sysadmin needs to change a configuration (perhaps a network setting), they must edit a text file with a tool such as Vim.

Vim's use is not limited to sysadmins, however. Developers and standard users can get a great deal of mileage from this editor. I use it regularly on my Mac to create basic files quickly (such as the outline for this article) or as a way of generating markdown files that I need for various projects.

While there is no certification course for Vim, it's an assumed skill for the CompTIA Linux+ exam, and the security professionals that pursue CompTIA Security+, CompTIA Cybersecurity Analyst (CySA+), CompTIA PenTest+, CompTIA Advanced Security Practitioner (CASP+) and other certifications will use Vim to accomplish their tasks.

Download the Vim Cheatsheet here.

So, what's the best way to get started using this powerful editor? I suggest beginning with four fundamental tasks to manage any file:

  1. Create/open
  2. Edit
  3. Save
  4. Quit

When working with a file, you must create or open it, make your changes, save them and then quit the Vim editor.

Install Vim

Before beginning, install Vim on your system. You may use Linux, Windows or macOS. Be aware that the steps vary a bit between versions and operating systems, so be ready to read the documentation specific to your system.

Vim on Linux

It's likely your Linux distribution already includes Vim, but if it does not, follow these steps to install it.

If you use a Linux distribution such as Fedora or CentOS, type:

$ sudo yum install vim

If you use a Linux distribution such as Ubuntu or Debian, type:

$ sudo apt install vim

The system should either install Vim or report that Vim already exists.

Vim on Windows

Windows users can download Vim from the official download page.

Vim on macOS

Mac users can download MacVim from the same official download page.

Alternatively, if you use the Brew package manager, type:

$ brew install vim

Once Vim is installed, you may follow the steps below.

4 Basic Vim Tasks

To illustrate these 4 skills, I will step through a scenario that creates a file, edits the contents, saves the changes and exits Vim. You must know these 4 skills; anything else you learn about Vim in the future simply makes you more efficient.

1. Create or Open a File

Using your chosen operating system, open a command line environment. In Linux, this is the Bash terminal. On Windows, you will probably select PowerShell. On a Mac, choose the Zsh terminal.

Next, browse to a directory you can use to create test files. Usually, whatever "home folder" your system gives you is fine.

To create a new file named demo.txt and open Vim simultaneously, type:

$ vim demo.txt

Remember, this may look a bit different on each platform.

Congratulations! You've accomplished the first step: Create a file.

Vim Command or Key


vim demo.txt

Create or open the demo.txt file in Vim

Vim Modes

Before you can begin entering text into your file, you must understand Vim modes. Because Vim does not use a mouse and offers no menus, the only way to issue commands such as "save" or "quit" to Vim is via the keyboard. However, the keyboard is also how you type text in a file. So how does Vim know the difference between a command and words being typed in the file? The answer is by switching the keyboard between modes.

Vim opens in Command mode. That means you are issuing commands to the program if you type something on the keyboard. The keyboard is "mapped" for commands. Vim's other mode is Insert. When in Insert mode, the keyboard is "mapped" to enter text into the file.

There are two simple ways to switch between modes:

  • Change from Command mode to Insert mode: i
  • Change from Insert mode to Command mode: ESC

There are many ways of switching from Command mode to Insert mode. I am showing one easy-to-remember way. When you get more comfortable with Vim, you will learn other ways. Also, keep in mind that Vim commands are case-sensitive.

So, you created the demo.txt file and opened it in Vim. By default, you are in Command mode. Before typing text into the file, you must switch to Insert mode. Do you remember the command?

Vim Command or Key



Switch to Insert mode


Switch to Command mode

2. Edit a File

From Command mode, type i to switch to Insert mode. Notice the "-- INSERT --" flag in the lower left corner. You may now enter text into the file. Type the following into your file:

The lower-case i switches to Insert mode.

The ESC key switches to Command mode.

Good! Your file now has contents. But wait! How do you save these edits? First, you must switch back to Command mode. Which key is that again?

3. Save a File

Press ESC to ensure you are in Command mode. Now it's time to save your changes. In Vim, you don't "save" changes; you "write" them. The :w command writes your changes to the disk. Notice the : character. That switches to a third environment, Execute mode. Execute mode places a command prompt at the bottom of the screen and is enabled with the : key.

In Command mode, press :w to save your changes.

Return to Insert mode and enter another line of text:

To save your changes, switch to Command mode and type :w

Return to Command mode and save the file with this new line.

Vim Command or Key



Save or write changes to the file

4. Exit Vim

At this point, you've created a file, switched modes, entered a few lines of text, and saved your changes. How do you get out of Vim?

To quit Vim, switch to Command mode and type the :q command. This returns you to your system's command line environment.

There are a few useful tricks here. Frequently you'll want to both write your changes and quit Vim. Using the methods above, you would type :w (to save) and then :q (to quit). However, you could simply type :wq to both write and quit using a single command.


One of the first places I got trapped when learning Vim was exiting Vim without saving changes. Vim displays a red error if you try to quit without saving, and it refuses your :q command. To quit a file without saving changes, type :q! while in Command mode.


Vim Command or Key



Save or write changes to the file


Exit Vim


Save changes and exit Vim


Exit Vim without saving changes

What's Next?

It's difficult to overstate how powerful Vim is and how many useful options exist. That's both the benefit and problem: Vim has so many choices that beginners are often overwhelmed. Hence, I advise learning the four common tasks before worrying about tips and tricks.

Let me suggest just a few useful features you can experiment with.

Display Line Numbers

Developers and sysadmins alike will appreciate having line numbers displayed along the left side of files. You might receive instructions that say, "edit line 42" and by default, Vim does not show line numbers.

To display line numbers in the current Vim session, enter Command mode and type:

:set number

Use the :set nonumber command to turn off line numbers.

File Navigation

You can navigate within a text file using the arrow keys. You can also use the k key to move up and j to move down. Move left with the h key and right with the l key. These navigation keys keep your fingers on the keyboard, making your typing more efficient.

Navigation or arrow keys are easy if the file is short. However, configuration files can be very long. During the editing process, you may need to move to the top or bottom of a file or to a specific line number.

To jump to the top of the file, switch to Command mode and type:


To jump to the bottom of the file, switch to Command mode and type:


To jump to a specific line number, such as line 42, switch to Command mode and type:


Search a File

Another useful trick is searching for a keyword or string of text. For example, to search for the "hostname" keyword, enter Command mode and type:


This highlights all instances of the keyword. Use n to cycle forward through the results and N to cycle backward through the instances. Type :nohl to turn off highlighting.

This search is case-sensitive.

Vim Command or Key


:set number

Display line numbers

:set nonumber

Do not display line numbers


Jump to the top of the file


Jump to the bottom of the file


Jump to line 42 of the file


Search for the string "hostname" (case-sensitive)

Find the Time to Use Vim

In many ways, Vim could be considered an archaic program. However, over the years, I've found myself using it more. Vim is one of the first programs I check for on my newly installed Linux systems. It's also something I routinely add to my Macs. It's not a replacement for a powerful word processor, but it certainly has its place with power users, developers and particularly administrators. The program is highly extensible and customizable, so once you get comfortable with the four standard tasks, you can begin to explore the options.

Install Vim today and begin learning the basics of this powerful editor so you can apply your knowledge to Linux configurations throughout the CompTIA certification exams.

Ready to get started? Download the CompTIA Linux+ exam objectives for free to see what's covered.

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment