Activity 2-1 - Introduction to VIM



The course of Computer Science I is designed to teach you programming, among the other things. One of the most important tools you need in order to do that is an editor and a compiler. Today's lab will introduce an editor to you known as VI or VIM (vi iMproved). The vi editor is a text-based editor used by Linux and Unix users. The vi editor has very powerful and dynamic features to aid programmers. Unfortunately, most new users try to avoid vi due to it’s complexity. The vi editor is essential for anyone working on a Linux or Unix platform. Linux/Unix users can usually avoid it for a while, but eventually, they will find that they have to learn it to do their jobs.

For this Lab you will prepare a lab notebook. Name the file YourNameNotebookLab2.

Our activities will be performed on the remote computer loki.cs.kent.edu.

Start up ssh or if you prefer any other secure shell such as PuTTY.

Log in to your account on loki.cs.kent.edu using the account name that you learned in the last lab (it begins with "ll") and the new password you set.

Backspace Setup

Some problems you may encounter when you first access your Computer Science account.

The backspace key will not work on your account. However, the delete key will work at this time. To fix this, at the $ prompt type in:

stty erase ^h

Then press the ENTER key. This needs to be done every time you log in, even if it is in a new window.  If you backspace key already works, then you can skip this step.

Remember that loki is a machine running a Unix operating system. If you prefer to refresh the basic commands to work under Unix (ex. how to list the files, how to create a directory, how exit from a directory, ect. ), many "cheat sheets" for Unix exist (one is here).

Starting vim

At the $ prompt, issue the command: vim practice.txt

Record in your lab notebook what was printed on the screen.

Perhaps the most important thing about this editor is to remember that vim has two modes:

1) Input or append mode : invoked by pressing i and it is used when you want to type . When you are in the insert or append mode, you are typing in information into the file. This is where you input information into a file. You will also see the change, replace, and open modes used here. These are other ways to type in information into a file.

2) Command mode : invoked by pressing the Esc key, and it is used to do other things, except typing, for instance; moving up-down, left-right, deleting characters, words, lines, saving, exiting, etc.  This is where you want to make changes in the file that you have been typing. The vi editor is a full screen editor. You can go over the entire document to make changes.

Adding Text

Type the following 4 lines of text exactly the way it is shown below (errors and all). To start entering text, be sure that you are in insert mode first.  To do this, press i once to enter the Input mode, and type the following text.

"Depends on the size of the machine, a UNIX mashine can support
from one to handred users. Each user can run different set of programs.
Since a UNIX machine can support several users at the same time,
the cost of computer per usr is much les. "

Saving a File

To save what you have typed press :Esc to go to command mode, then type: :wq text1 and press Enter(don't forget the : before the w) you are back at the command prompt again, $.

Open vim and closing

Open your practice.txt file in vim again by typing in vim practice.txt.

Add a new line at the end of the file that states.

"Today I am learning vim."

When you want to leave vim and close the file you use the quit command.  Be sure to enter into the command mode first by using the Esc key.

:q

This allows you exit vim.

Type in the :q command and record in your notebook what happens when you try to exit vim without saving.

To exit vim discarding any unsaved changes (this can happen if you make a mistake and DON'T want it saved) use the command

:q!

This quits without saving any change (don't forget the : and the !).

You are back to the prompt.

Open vim and save a file

Open up vim (again), typing vim practice.txt.

Record in your notebook any observations about the file content you have after closing using the :q! command.

Add a new line to the top of the file with your name on it.  To save this file without quitting, we use the write command. Again, be sure to hit Esc to enter into command mode before you type

:w

Record in your notebook what flashes along the bottom of the screen when you type in the :w command.

Searching Text

Vim is a fully featured editor, even if it does not have a graphical user interface (GUI). 

We want to find the mashine in the text, so we need to type :/mashine (don't forget the : and the /)

The command to search is :/ and then the text you are searching for immediately after.

Replacing (Overwriting) Text

Moving over a letter and replacing it with another one.  Use the arrow to move on the letter s in the word mashine, press r (replace) and press c.

Note: in the command mode, letters h, j, k, l can be used for navigating left, down, up, and right, respectively.  Some keyboard may not have arrows, or the arrow keys don't respond, and this may be the only way to navigate through vim.  Try to navigate a little using h, j, k, l  in the command mode.

Getting to a Specific Line and Deleting

To go to a particular line, for instance line 2 type :2 (don't forget : before 2).   A colon and the line number will take you directly to that line.

To delete a character and type a new one in its place we use the x command.

Move on the letter a in handred, press x to delete the single character a, press i to switch to input mode, type u, press Esc to switch to the command mode.

<Save the file by typing in :w.>

To move to the end of the file : press Shift and G

Use arrow to move the cursor to the beginning of the word usr.  To delete the word usr press dw (this will delete the word in front of cursor).

Press i and type the word user, then press Esc to switch to command mode.

Note: The D command will delete everything from there to the end of the line.

Appending onto Text

To append s after the word les, move to s and press a (append), and type the missing s, press Esc.

Record in your notebook how the insert (i) command differs from the append (a) command.

Delete and Move an Entire Line

Go to line 1.  We want to delete the entire line (which is really your first and last name) by typing in

:1

While still in command mode, type

dd

To move to the end of the file : press Shift and G

To move to the end of the line, type )

To put that line, type p

Record in your notebook whether the line was inserted before or after the last line of the file. 

Note: to delete multiple lines at once, type in the number of lines you want to delete before typing in dd.

Undo

If you have just mistyped something or want to undo a command (while in command mode).  Undo the last two changes (i.e. your name should be at the top of the file) by typing

:u

:u

while in the command mode.

:u Undo last change
:U Undo all changes on line

Exiting the Editor

Save the file and exit by typing :wq (remember the : before w)

Congratulations! You just finished the basics of what you need to know to work with vi and vim editors.

Vim Commands

There are many, many more options and ways to use vim that have not been discussed here.  There are many "cheat sheets" for vim that exist (one is here).  By looking at a few, you will see how much more depth vim has.

Quick Reference Sheet: http://www.digilife.be/quickreferences/QRC/VIM%20Quick%20Reference%20Card.pdf

http://fprintf.net/vimCheatSheet.html

Vi Command Description
yy followed by p command  the yy command copies the current line and p command will paste it at as new line after the cursor
yyn Yank n lines (default is 1) starting from the current
p Put after the line where the line(s) you yanked right cursor is
$ End of line
End of the word to the right
o Open a line below the current line
O Open a line above the current line
x Delete the character
nx Delete the number of characters specified by n, starting with the current
u Undo the change
ddn Delete the current line
) End of sentence
( Beginning of sentence
} End of paragraph
{ Beginning of paragraph
0 Beginning of line
A Add text at the end of the line
j Move down
h Move Right
k Move Up
l Move Left