[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci201] / vi
[Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01]
Mon Dec 17 15:21:01 PST 2007

Contents


    Beginning Vi

      Facts

      vi is designed for good typists.
      1. Cursor movements are done by typing normal characters.
      2. Commands are done by typing normal characters.
      3. Text is input by typing normal characters.


      1. No mouse needed.
      2. No menus needed
      3. No special keyboards... just a typewriter keyboard
      4. No windows needed
      5. What you type is not what you see, and not what you get!

      It is probably the oldest widely-used editor

      It is used in every UNIX establishment in the world.

      It runs on every platform in the world.

      If it suits you, it is a very good editor. If not it is the only emergency editor you've got.

      Making Vi easier to start with

      Input the following UNIX command
       		~dick/bin/Q novice
      and you will reprogram vi to start in novice mode. vi will continue in this mode until you:
       		rm .exrc; ~dick/bin/Q setup
      or until you change '.exrc' in some other way.

      Introduction

      You can not input characters until you type the 'i' for input command.

      You type in commands. Some commands let you input characters. The input characters end when you tap the esc key.

      Commands are keystrokes:abcdefg...ABC....Z(){}$0!/?<>...

      You can not, on a networked connection trust use the arrow keys. The are transmitted as a sequence of characters and there can be a delay.

      Do these exercises slowly once. And then repeat more quickly. The goal is learn the key strokes not to finish quickly. -- vi is the thinking person's editor. You need to train your fingers to drive the editor.

      Exercise 1: Getting started

      Put a piece of paper over the arrow and number keys! Hide that mouse!

      In a terminal window ('xterm' is best) or on a terminal at home, at the unix prompt type in the following: (esc means tap the Esc key, Tap return at the end of each line).

       vi ex
       iThis is some input.
       And here is some more.
      esc
        If you forgot the 'i' on the second line above then then you will get a few beeps and the text "s is some input..."! The best thing to do is to exit 'vi' discarding the text:
         :q!
        
        and repeat the exercise from the beginning.
        

      You have just used the 'i' command to input some text. Next you will go up to the first line, delete the first line and then paste it back as the last line:

       kddp

      Do the same keys again and watch: k = up, dd = delete line, p=paste after.

      We will now delete the word 'And' and change the 'h' to 'H':

       dw~

      We will no Go to line 1:

       1G

      We will now find the word 'some' and delete it.

       /some
       dw

      We will not do the same thing again - find next 'some' and delete it:

       n.

      Now put it back by undoing the delete: u=undo

       u

      We will now undo the undo:

       u

      (If you hold down 'u' the word will be inserted and deleted repeatedly)

      We now find can change the word 'input' to 'text':

       /input
       cwtext
      esc

      Add a line at the end of the file:

       GA
       These are the last two
       lines in the file.
      esc

      We now save (write) the file and quit(exit):

       :wq
      Unix Prompt...
       cat ex

      You've just used these commands:


      1. A=Add After line.
      2. cw=change
      3. dd=delete line
      4. i=insert
      5. k=up
      6. G=Goto
      7. n=Next
      8. u=undo
      9. w=word
      10. :wq=write and quit
      11. /=search
      12. .=do-it-again
      13. ~=change upper case to lower case and vice versa.

      Remove the ex file and repeat the above exercise.

      Exercise 2: Input and Sort a file

      Input the following and see watch what happens:
       vi ex2
       iSpot
       Dick
       Jane
      esc
       1G!Gsort
       :wq
      
      

      Exercise 3: Adding data

      Use these keys in vi to add numbers to 'ex2' file:

        return = goto next line
      1. A = Add At end of Line
      2. . = do it again
        esc = end my added text

       vi ex2
      return
       A	122-45-6789
      escreturn
       A	123-45-6789
      escreturn
       .:wq
      return

      You should end up with:

       Dick	122-45-6789
       Jane	123-45-6789
       Spot	123-45-6789

      Adding new lines:

       vi ex2
      return
       oDoc	987-65-4321
      return
       John	987-65-4321
      esc
       :w
      return

      Sorting the data again:

       1G!Gsort
       :wq
      return

      Exercise 4: Cursor movements

      This is an exercise in moving the cursor around, one character at a time, without using the arrow keys. First we input and save a 100><100 grid of plus signs - in fact 10 lines each with 10 plusses:

       vi grid
       10i+
      esc
       ..........
       :w

      Put a finger above the 'k' and watch what happens as you push it 10 times. Then watch what happens when you tap the 'l' key 10 times. Then do 10 j's and then 10h's. Repeat this three times, speeding up as you go. It should teach your fingers about left-down-up-right movements of the cursor.

      You can also repeat most commands without typing them in many time:

       10k10l9j10h

      You can replace a plus sign on the screen by another character using the r command followed by the new character... for example: (with no return at the end...)

       5k5lro

        Goes up 5, right 5 and replaces the character by a 'o'.

      So by moving around the grid an replacing characters can you make a drawing of something? A face, a traffic sign. etc?

      You can save the your picture at any time by

       :w
      (with return) You can quit by
       :q

      You can post the saved file on the BBS... and you can even send it directly to the BBS like this:

       1G!Gpost
      which reads:
      1. 1, GO
      2. Escape Go to end
      3. post it

      Exercise 5: Cursor Movements and gemini:/usr/games/hack

      The exists a game that uses the same cursor keys as 'vi':
      1. h::vi_movement=left.
      2. j::vi_movement=down.
      3. k::vi_movement=up.
      4. l::vi_movement=right.

      (So does the WWW browser 'lynx', the file browser 'less', etc...) The game is a distant ancestor of DOOM and QUAKE... you run about inside a dungeon destroying monsters, casting spells, collecting various things, and other kinds of fun.

      You will be shown a map of parts of a dungeon on the screen and each time you tap a key it will be read as a command. The commands move you about the dungeon, attack monsters, and pick up various things. It has lots of variations. The original was called 'rogue', a later one was called 'urogue' and the 'hack'. These days you can download 'nethack' (an open source version) for Windows etc.

      Hint: Don't kill the little dog or pony.

      Exercise 6: Handling words

      This exercise is about moving about a piece of text one or more words at a time, and changing one or more words. The commands are
      1. w = skip one word forward
      2. b = skip one word back
      3. + = start of next line

      4. dw = delete next word
      5. cw = change word

      Input the following as shown below, including tapping the Return key at the end of each line:

       vi words
       iThe quick brown fox jumps over the lazy dog.
       Pack my box with five dozen liquor jugs!
       Now is the time for all people to come to
       the aid of the party.
      esc
       kkkk
      You should now have the cursor at the start of the text. We will first slow down the fox and then speed up the dog: (NO returns at end of line below...)(watch what happens as you type this... but don't react to it)
       wcwlazy
      esc
       6wcwquick
      esc Did you notice how the change command 'c' put a dollar sign at the end of the word being changed?

      The following shows how 'b' is used...

       bbbbbbcwquick
      esc

      Next we will apply prohibition to our jugs:

       +6wdw

      Now a little experiment. watch what happens when you tap the 'w' key until you get to the end of the file, and then 'b' until you get back to the beginning... Answer this question: Are punctuation marks like "!" and "." treated as words or as part of words?

      6 Free Practice.

      Use the key commands covered in these exercised to create a message celebrating you newly learned skill and post it on the bulletin board BBS. Keep it short and clean!

      Special symbols in these exercises

    1. esc::=tap the Escape key.
    2. return::=tap the Return or Enter key.

      See Also

      [ vi ] [ vi.commands ] [ vi.FAQ ]

    . . . . . . . . . ( end of section Beginning Vi) <<Contents | End>>

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End