Creating a file
- 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.viis 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.
- 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.
- 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.
- :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 : tellsviyou'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 quitvi. So you should be back at the shell prompt.
- cat testfile
cat will display the file you typed on the screen.
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'svi 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.- vi testfile
Open the file you created earlier withvi. You should see the text you typed before.
- Move around the file with the arrow keys or the hjkl keys. If you try to move to far in any direction,
viwill beep and refuse to do so; if you want to put text there, you have to use an insertion command like i or a.
- :q
Exitvi.
Deleting text
- vi testfile
Open your practice file again.
- dd
The dd command deletes a line; the top line of the file should be gone now. [Note: u -undo action.]
- x
x deletes a single character; the first letter of the second line will be erased. Delete and backspace don't work invi, for historical reasons[13]. Somevivariants, such asvimwill let you use backspace and delete.
- 10x
If you type a number before a command, it will repeat the command that many times. So this will delete 10 characters.
- 2dd
You can use a number with the dd command as well, deleting two lines.
- :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:
- :q!
With an exclamation point, you tellvithat 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.
- 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!.
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