Saturday, February 28, 2015

vi Editor: Useful concepts


Creating a file

  1. vi testfile
    In your home directory, invoke vi by typing vi followed by the name of the file you wish to create. You will see a screen with a column of tildes (~) along the left side. vi is now in command mode. Anything you type will be understood as a command, not as content to add to the file. In order to input text, you must type a command.
  1. i
    The two basic input commands are i, which means "insert the text I'm about to type to the left of the cursor", and a, which means "append the text I'm about to type to the right of the cursor". Since you are at the beginning of an empty file, either of these would work. We picked i arbitrarily.
  1. Type in some text; here's a profound statement from philosopher Charles Sanders Peirce, if you can't think of your own:
         And what, then, is belief? It is the demi-cadence 
         which closes a musical phrase in the symphony of our 
         intellectual life.  We have seen that it has just 
         three properties: First, it is something that we are
         aware of; second, it appeases the irritation of doubt; 
         and, third, it involves the establishment in our 
         nature of a rule of action, or, say for short, a 
         habit.
    
    Press RET after each line, since vi will not move to the next line automatically; when you finish typing, press the ESC key to leave insert or append mode and return to command mode.
  1. :wq
    If you've done everything correctly, when you type this command it should appear at the bottom of your screen, below all the ~ characters. The : tells vi you're about to give a series of commands; the wmeans to write the file you've just typed in --- in most new programs this is called "save" --- and the q means to quit vi. So you should be back at the shell prompt.
  1. cat testfile
    cat will display the file you typed on the screen.
Don't remove testfile, we'll use it in the next tutorial section.
As you use vi, always remember that pressing ESC will return you to command mode. So if you get confused, press ESC a couple times and start over.
vi has an annoying tendency to beep whenever you do something you aren't supposed to, like type an unknown command; don't be alarmed by this.

Moving around in a file

To move around in a file, Debian's vi allows you to use the arrow keys. The traditional keys also work, however; they are h for left, j for down, k for up, and l for right. These keys were chosen because they are adjacent on on the home row of the keyboard, and thus easy to type. Many people use them instead of the arrow keys since they're faster to reach with your fingers.
  1. vi testfile
    Open the file you created earlier with vi. You should see the text you typed before.
  1. Move around the file with the arrow keys or the hjkl keys. If you try to move to far in any direction, vi will beep and refuse to do so; if you want to put text there, you have to use an insertion command like i or a.
  1. :q
    Exit vi.

Deleting text

  1. vi testfile
    Open your practice file again.
  1. dd
    The dd command deletes a line; the top line of the file should be gone now. [Note: u -undo action.]
  1. x
    x deletes a single character; the first letter of the second line will be erased. Delete and backspace don't work in vi, for historical reasons[13]. Some vi variants, such as vim will let you use backspace and delete.
  1. 10x
    If you type a number before a command, it will repeat the command that many times. So this will delete 10 characters.
  1. 2dd
    You can use a number with the dd command as well, deleting two lines.
  1. :q
    This will cause an error, because you've changed the file but haven't saved yet. There are two ways to avoid this; you can :wq, thus writing the file as you quit, or you can quit without saving:
  1. :q!
    With an exclamation point, you tell vi that you really mean it, and it should quit even though the file isn't saved. If you use :q! your deletions will not be saved to testfile; if you use :wq, they will be.
  1. cat testfile
    Back at the shell prompt, view testfile. It should be shorter now, if you used :wq, or be unchanged if you used :q!.
:q! is an excellent command to remember, because you can use it to bail out if you get hopelessly confused and feel you've ruined the file you were editing. Just press ESC a few times to be sure you're in command mode and then type :q!. This is guaranteed to get you out of vi with no damage done.
You now know everything you need to do basic editing; insertion, deletion, saving, and quitting. The following sections describe useful commands for doing things faster; you can skip over them if you like.

No comments:

Post a Comment