CSCI-1411-Using-vi

Quick Guide to Using Vim

This guide describes the text editor vi and provides a step-by-step guide to creating a C++ source file, editing the contents, and then saving the file. vi is a command-based text editor that provides advanced features for quickly editing text-based files from a terminal (command-line) interface. This program can be used from the command line by simply enter the command: vi. This will run the program and you can edit and save any kind of basic text file: text files (*.txt), C++ source files (*.cpp) and C++ header files (*.h). vi is included with almost every linux distribution (or some closely related alternative) and is pre-installed.

This guide will show you how to open the vi application, create a C++ source file, save the file, and then exit.
If you would like additional information about vi then there are several online tutorials. One of which is an interactive vim (note vi and vim are similar, vim is an extension of vi) emulator that allows you to practice using vim in a browser: Open VIM

Vim – A Command-Based Text Editor
When you first start using Vim you will notice that it is different than most simple text editors. Specifically if you open the program and start to type, the application will not enter any of the keys you are pressing. This is because Vim operates in two different modes: Command Mode and Insert Mode. Getting used to switching between these modes will take some time if your not familiar with this type of interface. Below is a brief description about both of these modes:

  • Command Mode: This mode is used to send commands to the Vim application. For example saving and quitting. To enter command mode press the Esc key.
  • Insert Mode: Allows you to enter text as you would in any text editor. To enter insert mode press the i key (i for insert).

HorizontalLine

Using Vim to Create a C++ Source File

Step 0: Connect to the Unix CSE grid and login (The CSCI-1411 page contains guides on how to do this for both PC and Mac computers). Once you are at the Unix command line you can proceed to Step 1.

Step 1: Open the vi editor using the following command: vi

Open vi (Enter the command: vi)

Open vi (Enter the command: vi)

Step 2: Once you have vi open, you will be shown the following screen. The application is now open and can be used to edit and create files. When you open Vim, the default mode is the command mode. If you type at this point it will not be entered into the Vim editor since it is in command mode.

Vim Default Screen

Vim Default Screen

Step 3: To insert some text into a new file press the i key to enter insert mode. After pressing the i key you will be able to type in the Vim editor. In this example a simple C++ Hello World program is typed into the editor.

Vim Simple Hello World Program

Vim Simple Hello World Program

Step 4: Once you have finished editing the files hit the Esc key to enter command mode. You can now save the file by typing the following command: :w test.cpp and hitting enter (where test is the name of the file you want to save this text to). Note: If it says that there is an overwrite problem then simply use the command: :w! test.cpp to force Vim to overwrite the file.

Saving in vi (:w filename)

Saving in vi (:w filename)

Step 5: To exit vi, ensure you are still in command mode and enter the following command: :q and press enter. This will close the vi editor.

vi Quit Command (:q)

vi Quit Command (:q)

If you have completed this guide then you should have a new test.cpp C++ source code file that you can compile and run.