|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ISBN: 3836213664 ISBN: 3836213664 ISBN: 3836213664 ISBN: 3836213664 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Wir empfehlen: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A free Basic UNIX-training with muLinuxIn order to save this exercise to floppy disk, insert the floppy in the drive, mount the drive to /a and cd to /a./# mount /dev/fd0 /a /# cd /a /a#
All configuration files in Unix are text files. It is therefore
neccesary to understand the usage interface of a text editor that is
available on every Unix system. MuLinux is particularly useful as a
rescue system, e.g. when you've forgotten an important password, so at
least you want to be able to make minor changes using
vi. Have no fear, we're not going to write novels. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "text.txt" [New File] 1 line, 1 char |
The last line is for passing information to and communicating with the user. In this example the file name is displayed, followed by the information that the file was newly created, and the number of lines and characters therein.
[Esc] :w [Return]
The [Esc] key switches to command mode. The colon switches to last line mode, a special command mode for communicating with the user on the last line. In last line mode we enter the command w, which is short for write. The [Return] or [Enter] key ends last line mode. This probably sounds slightly weird.
When working with vi you should always have a finger near the [Esc] key. The author usually uses the fourth finger of the left hand.
[Esc] : q [Return]
/a#
In place of w, use q, for quit. Note that vi commands are case sensitive.
The last line once again shows the blinking muLinux cursor. Using the command ls we can check that the file test.txt was indeed created.
/a# ls
test.txt
Using the command history (cursor up key) or otherwise let's repeat the command vi text.txt.
/a# vi test.txt
In command mode incoming characters are interpreted directly as commands to the editor. There is no prompt (meaning you don't know you're in command mode before you type the key) and no echo (meaning you can't neccesarily tell that something happened).
last line mode is the only mode in which a prompt is displayed, namely the colon ":". The prompt is visible in the status line (the last line), together with the commands you type and output from these commands.
In text mode you can edit your text. All characters you enter will be interpreted as characters in the current file.
When first invoked, vi is in command mode. You cannot enter text in command mode.
What you can do in command mode is move the cursor to any position within the current file using the cursor keys (if you have any), or a number of other keys. You can change to another operational mode at any time.
You can enter text mode by typing certain command letters. The author usually types i for insert. You should definitely remember the letter i.
i
Now you can simply type your text, beginning new lines by typing RETURN. ~ ~ ~ ~ ~ ~ ~ ~ ~ |
Push the Esc key to get back to command mode
[Esc]
And let's save again.
[Esc] :w [Return]
Oops, we typed [Esc] twice running... Doesn't seem to matter. Better type it once too often than leave it out when you need it. That way you can be quite sure you left text mode.
Let's continue with our text. Here's a list of all commands for switching to text mode:
| [Esc] | i | Insert text before current cursor position |
| [Esc] | I | Insert text at beginning of line |
| [Esc] | a | Insert text after current cursor position |
| [Esc] | A | Insert text at end of line |
| [Esc] | o | Insert new line after current line |
| [Esc] | O | Insert new line before current line |
Type a few more lines and save your prose.
| [Esc] | u | Undo the latest change |
| [Esc] | U | Restore the complete line |
The author believes you will quickly get used to these commands when working with vi.
Using the following commands you can then move through your letters and invoices. You only need to press [Esc] once, of course, to get into command mode.
| [Esc] | h | Move cursor one character to the left |
| [Esc] | j | Move cursor one line downwards |
| [Esc] | k | Move cursor one line upwards |
| [Esc] | l | Move cursor one character to the right |
As you see, these keys are arranged in one line on your keyboard, ideally suited for the fingers of your right hand.
| h | first finger | |
| j | second finger | |
| k | third finger | |
| l | fourth finger |
Close to on the upper row of your keyboard you will also find the important keys i (insert mode) and u (undo), easily reachable with second and third finger. You needn't move your hand off the letters to get at the cursor keys. Using these keys vi-pros find they can work very quickly.
Some more useful keys for navigation are:
| [Esc] | w | Move cursor one word to the right |
| [Esc] | 3w | Move cursor three words to the right |
| [Esc] | b | Move cursor one word to the left |
| [Esc] | 7b | Move cursor seven words to the left |
| [Esc] | [control] F | Move forward one screenful (page down) |
| [Esc] | [control] B | Move backward one screenful (page up) |
| [Esc] | 0 (zero) | Move cursor to beginning of line |
| [Esc] | $ | Move cursor to end of line |
| [Esc] | 1G | Move cursor to beginning of file |
| [Esc] | G | Move cursor to end of file |
Don't worry if you find you cannot remember all these commands at once. The author suggests you practise by opening a lengthy file, for instance the file /usr/doc/readme.html might be appropriate. First of all save your poetic creation ([Esc] :w [Return]) and exit the editor ([Esc] :q [Return]). Then open said readme-file.
/a# cd /usr/doc
/usr/doc# vi readme.html
Should you mistype the name, vi will create a new file. In that case you can exit and try again. When you finally see /usr/doc/readme.html on your screen, try navigating the text using the command letters as explained. N.B. this file is in HTML format, it is a web page.
[Esc] :q! [Return]
/a#
Don't try to learn the remaining commands at first. It is better to practise them now and then. For now, concentrate on understanding the operational modes of vi.
Words of wisdom Only the good, why run? Waste not, want vodka Walker over troubled bridges Ten Beers After My home is my beer castle. Two beers or not two beers - that is the question! Shakesbeer ~ ~ ~ ~ ~ ~ ~ |
The search commands are usually introduced with a slash /. As soon as you type the slash, it is displayed on the last line and the cursor becomes visible after it. You can then enter the text you wish vi to search for. Mark the end of the search string by hitting [Return].
In order to search backwards (towards the beginning of the file), use a question mark ? instead of the slash.
| [Esc] | /<expression> | Search for expression (forwards) |
| [Esc] | ?<expression> | Search for expression (backwards) |
| [Esc] | n | Continue search (for next match) |
| [Esc] | fX | Simple search: find next occurance of character X |
To search for the word "milk" in the text, enter the following
[Esc] /milk [Return]
If you want to search for several possible characters at the same time, these should be placed in square brackets [ ]. The following command searches for both "milk" and "mild" at the same time.
[Esc] /mil[kd] [Return]
You can also specify ranges. To accept all four letter words that begin in mil and end in one of the letters d through k, enter:
[Esc] /mil[d-k] [Return]
The square-bracket-construct is called "character class". You can specify characters to be excluded from the match, by using the character ^ inside a character class. In the following example the word is allowed to begin with anything but a lowercase letter:
[Esc] /[^a-z]ilk [Return]
You can match any character (including space) with the period character ..
[Esc] /.er [Return]
You can search and replace using the command s/ which accepts the following syntax:
[Esc] :[fromline,toline]s/searchtext/replacetext/[gc] [Return]
From- and toline may be ommited, in which case the search takes place only on the current line. Fromline is the first line searched, toline is the last line searched. The following special characters can be used: The period . indicates the current line. The dollar sign $ indicates the last line of the file.
The option g at the end of the command tells vi to replace all occurances of the pattern. Without g, only the first occurance found is replaced.
The option c at the end of the command tells vi to ask for confirmation before replacing each occurance.
In this example all occurances of "Labour" are replaced by "Tori", while asking for confirmation at each change.
[Esc] 1,$s/Labour/Tori/gc [Return]
Depending on where you life, you may want to try one of the following lines instead.
[Esc] 1,$s/Democrat/Republican/gc [Return]
[Esc] 1,$s/SPD-Grüne/CDU-FDP/gc [Return]
| [Esc] | dd | Deletes the current line |
| [Esc] | 5dd | Deletes the next five lines |
| [Esc] | dw | Deletes from the cursor position to the end of the current word |
| [Esc] | dG | Deletes from current line to last line, all inclusive |
| [Esc] | d0 | Deletes from the cursor position to the beginning of the line |
| [Esc] | d$ | Deletes from the cursor position to the end of the line |
| [Esc] | d1G | Deletes from current line to first line, all inclusive |
| [Esc] | dfX | Deletes from the cursor position to the next occurance of the character X |
Here are some more deleting commands:
| [Esc] | x | Delete the character under the cursor |
| [Esc] | J | Delete the newline character, thereby joining the current line to the next |
| [Esc] | p | Paste contents of buffer after current line |
| [Esc] | P | Insert contents of buffer before current line |
In order to be able to copy a block of text, you need to first define one. The command [Esc] y does this for you. Like the delete command [Esc] d it needs to be given a positional argument.
| [Esc] | yw | Copy into the buffer from cursor position to end of current word |
| [Esc] | yG | Copy into the buffer from current line to last line, all inclusive |
| [Esc] | y0 | Copy into the buffer from cursor position to beginning of current line |
| [Esc] | y$ | Copy into the buffer from cursor position to end of current line |
| [Esc] | y1G | Copy into the buffer from current line to first line, all inclusive |
| [Esc] | yfX | Copy into the buffer from cursor position to next occurance of character X |
| [Esc] | yy | Copy current line to the buffer |
To insert the contents of the buffer, use the paste commands p and P explained above.
/# cd /usr/doc/help
/usr/doc/help# vi *
Inside vi you can go on to the next file with the command :n.
[Esc] :n [Return]
/# cd /etc
/etc#
/etc# cp issue issue.original
/etc# vi issue
We find a number of control characters, responsible for the colors and the Italian flag issued at login time. We want to delete all of that. With the cursor positioned on the first line of the file, key in the following command:
[Esc] dG
All lines from the current line to the last line are deleted. We can now insert ([Esc] i) a new welcome message. For instance
Lovely login prompt, that's what this is.
Quit vi and log out using the shell command exit. You should be greeted by your own lovely new login prompt.
/# cd /etc
/etc# cp ls.conf ls.conf.original
/etc# vi ls.conf
Delete all lines of the file and create just this one new line:
-l
Save the file and quit vi. Every time you type ls, the command executed will be ls -l.
This has only been a tiny extract of the full features of vi. There are many more commands and possible combinations, but these should be enough for a start.
Robert.Warnke@giso.de (copyleft) Robert Warnke, Berlin (Germany) - You can write me in English. | http://rowa.giso.de | translated by Mark Roberts, Göttingen (Germany)
|
Linux-Unix-Programmierung: Das umfassende Handbuch (Gebundene Ausgabe) von Jürgen Wolf | |
| Siehe auch: | |
![]() | |
| Shell-Programmierung: Das umfassende Handbuch | |
| C++ von A bis Z / Mit CD-ROM: Das umfassende Hand... | |
| C von A bis Z. Das umfassende Handbuch für Linux,... | |
| Qt 4 - GUI-Entwicklung mit C++: Das umfassende Ha... | |
| Eclipse für C / C++-Programmierer: Handbuch z... | |
| Linux: Das umfassende Handbuch | |