[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [Samples] / vi
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Thu Aug 25 21:04:50 PDT 2011


    Beginning Vi


      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.


      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.
        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:
        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:


      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':


      We will no Go to line 1:


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


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


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


      We will now undo the undo:


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

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


      Add a line at the end of the file:

       These are the last two
       lines in the file.

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

      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

      Exercise 3: Adding data

      Use these keys in vi to add numbers to 'ex2' file:
      1. return = goto next line
      2. A = Add At end of Line
      3. . = do it again
      4. esc = end my added text

       vi ex2
       A	122-45-6789
       A	123-45-6789

      You should end up with:

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

      Adding new lines:

       vi ex2
       oDoc	987-65-4321
       John	987-65-4321

      Sorting the data again:


      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

      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:


      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...)


        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

      (with return) You can quit by

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

      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.
      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)
      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...


      Next we will apply prohibition to our jugs:


      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.txt ] [ vi.commands.txt ] [ vi.FAQ.txt ]

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