Lesson 4 - The VI editor

 

There is no such thing as a real UNIX administrator who does not know how to use the vi editor.  vi is the default editor on every UNIX/Linux distribution.  Here are some reasons vi is so awesome:

 

1- It's not a GUI based editor.  As a UNIX/Linux admin, most of your work is done on remote hosts.  It's critical to be able to do this from an xterm/vt100 based terminal.

 

2- vi has lots of shortcuts.  While it is difficult to get used to using the vi editor, once you master some of the short cuts, it becomes very efficient to use.

 

3- vi is lightweight.  It doesn't take up a lot of memory or cpu. 

 

 

 

So, please read through the following instructions/shortcuts on VI.  I'd also like it if you used only the vi editor for your UNIX work until you become very comfortable with it.  One of the questions on most entry/mid level UNIX/Linux administrator jobs will be "what editor do you use"?  The correct answer will be either vi or emacs.  On the East coast, Emacs is only really popular among students from Rutgers University for some reason. 

 

 

http://www.eng.hawaii.edu/Tutor/vi.html   <- read this first

 

http://www.uic.edu/depts/accc/software/unixgeneral/vi101.html

 

 

I don't think anyone knows ALL the vi shortcuts.  I'll put a few of my notes here:

 

 

NOTES (they key strokes I use most often):

 

 

The following keystrokes are not normally used. Most people use the arrow keys on the keyboard:

h

move the cursor to the left one character position.

i

enter insert mode, the characters typed in will be inserted before the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.

j

move the cursor down one line.

k

move the cursor up one line.

 

 

Copy a line:

Move the curser onto the line you want to copy and type “yy”   (yank yank)

Paste the line:

Go to the place you want to put the line and type “p”          (put)

Delete a character:

x

Delete a line:

dd

add a line below the line with the curser:

o

add a line above the line with the curser:

O

 

These come in handy when you see error messages like “error on line 1298”

Turn line numbers on:

:set nu

Turn line numbers off:

:set nonu

 

Page forward:

<ctrl>f

Page back:

<ctrl>b

 

 

 

Assignment:

Type some of these into a file:

http://en.wikiquote.org/wiki/Caddyshack

 

 

 

PART 2 – Some UNIX Commands to learn:

 

Alias – create an alias for a command.  For instance:

        alias foo=”ssh tmurphy@myhouse.com

        instead of having to type the command, I can just type foo as a substitute now.  You would          

        normally add aliases to your .bashrc file

 

Tar –   tar was used to copy data to tape (tar = tape archive).  We now use it to combine               

        Directories into one file.  

 

        Create a tar file:

        tar –cvf <file.tar> <directory>   

        where c = create v= verbose f= file file.tar is the file.tar is the file you’re creating and           

        directory is the directory you are condensing to one file

 

        Untar a tar file:

        tar –xvf <file.tar>

        x=extract v=verbose f=file

 

        list whats in a tar file before you open it:

        tar –tvf <file.tar>

        t=tableOfContents v=verbose f=file

 

Gzip -  tar by itself doesn’t compress a file, it only makes it convinent for copying/ftp’ing etc.     

because compression is awesome, tar files are often compressed using gzip (there are other                                                                   UNIX compression tools, but we’ll learn those later).

 

Compress:

gzip file.tar

this creates a file called file.tar.gz

 

Uncompress:

gunzip file.tar.gz

leaves you with file.tar

 

Find – this finds files based on a number of criteria.  The syntax is as follows:

       find <path to start> -<criteria>

 

       for instance:

       find / -name passwd     means find starting at / a file named passwd

 

       the best way to learn the find command is by example. 

       this page has a number of examples, so you may want to keep it handy:

       http://content.hccfl.edu/pollock/Unix/FindCmd.htm

 

 

 

 

Assignment/Exercises:

 

1- Create a tar/gzip copy of your home directory:

cd /home

tar –cvf /tmp/username.tar /home/username

gzip /tmp/username.tar

 

2- use the tar file you created and make a duplicate of your home directory in /tmp

cd /tmp

gunzip username.tar.gz

tar –xvf username.tar

 

3- make aliases for “cd /etc” and “cat /etc/passwd” in your .bashrc   

 

4- use the find command to get all files in the /etc tree that are older then 20 days old