Understanding modes
Perhaps the most puzzling aspect of vi is that it is a modal editor. These are the two modes of operation:- Insert mode: If you're in insert mode and type an x, the letter is added to the document at the current cursor point.
- Command mode: If you're in command mode, the x command causes the letter under the cursor to be deleted, not added.
echo "set showmode" >> ~/.exrc
Do that once, and you've created a custom preferences file for vi
(yes, it should be called .virc, but that's a long story). You don't
have to ever think about it again.Starting vi
You can start the vi editor from the command line several ways:- Type vi on the command line:
$ vi
- Specify the name of an existing file to edit or a new file to create:
$ vi my.new.file
- You can also specify a list of filenames if you want. You can finish editing the first file, and then move to the second, and so on to modify a batch of files in sequence.
$ vi ashley.travels.txt
The File, Edit, View, and other menus are for the
terminal application, not vi. The vi program has no fancy interface
elements, just whatever you type at the keyboard.
There's no menu at the bottom, just a cursor at the top-left corner
and a bunch of tilde symbols (~) running down the left side. The lines
prefaced with tilde symbols are placeholders, not part of the file. They're beyond the end of the file being edited.Entering text
By default, the editor starts out in command mode: Type an x, and you'll hear a beep as the editor tells you that there's nothing to delete. You can move into insert mode several ways, depending on where you want to insert text. In fact, vi has dozens and dozens of commands.Following are a few some basic commands:
- To insert just before the current cursor location (where the flashing block is sitting), press i.
- To insert just after the current cursor location, use a to append text.
- To insert just above the current line by creating a new blank line, use O (capital o).
- To insert into a new blank line just below the current line, use o (lowercase o).
Gulliver's Travels is the most well-known work
of Irish writer Jonathan Swift, famous for his work as a novelist,
essayist and satirist. You Betcha!
To make this interesting, add a few random nonsense characters at the end of what you type in.Notice that the bottom-right corner of your screen says INSERT MODE. That's the show mode feature giving a visual clue of what mode you're in.
To leave insert mode and get back to command mode, press the magic key, Esc. Intelligently, the Esc key has no function in command mode. You can press it any time you want to be in command mode and aren't sure what mode you're in. It just beeps.
Moving in the file
Because vi has no scroll bars and no mouse support, it has a set of keys that you can use in command mode to move around.On Solaris, you can also use the arrow keys on your keyboard unless you're connected remotely, in which case they may or may not work.
The four key movement keys are h, j, k, and l:
- h moves one character to the left.
- j moves one line down.
- k moves one line up.
- l moves one character to the right.
- You can move one word at a time with w or b, depending on whether you want to move forward one word or backward one word.
- You can jump to the beginning of the line with 0 (zero) and to the end of the line with $.
- To move page by page, when the file is sufficiently large to have pages of text, use
• ^F to move forward a page
• ^B to move back a page
• ^D to move down half a page
• ^U to move up half a page
- You can jump to the first line of the file with 0G (zero followed by G) and to the end of the file with G by itself.
Finish up the delete process so that there are no stray characters. Then use the a append function to add a few carriage returns immediately after the period ending the sentence. The cursor should now be on the last line, with at least one blank line separating it from the text in the file.
Including other files
To include the contents of another file, you need to jump to the vi command line.- You do this by typing : , at which point the cursor immediately jumps to the bottom-left corner of the screen.
- Type in the following command:
r gullivers.travels.txt
- After pressing Enter, the contents of the file are injected into the buffer, exactly as you saw in the GNOME Text Editor and CDE Text Editor.
Saving files
The final step on this journey is to save the new file. You started vi with the new filename. To save the file with that name, type one of these commands:- :w writes the file and stays in vi.
- :wq writes the file and quits vi.
If you entered more than one file, use :w to write out this file and then :n to move to the next file on your list.
To quit and discard the changes that you have made, add a ! to the end of the command, like this:
:q!
Không có nhận xét nào:
Đăng nhận xét