Titel:

A free Basic UNIX-training with muLinux

Startseite
english
  
ISBN: 3836213664   ISBN: 3836213664   ISBN: 3836213664   ISBN: 3836213664 
 
  Wir empfehlen:       
 
[Back] [Home] [Forward]       http://rowa.giso.de  

A free Basic UNIX-training with muLinux

In 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# _

The Text Editor vi

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.

The Unix standard editor is vi, pronounced "vee eye". It is not only available in all versions of Unix, but also for Windows (http://www.snafu.de/~ramo/WinViDe.htm) and DOS. It is extremely versatile, but the user-interface is said to be rather unusual.

One reason for vi's uncomfortable interface is that it is designed to be usable on old terminals, which often have no cursor or function keys. Also function keys can be bound to miscelaneous functions and actions when logging into a remote machine. To overcome these handicaps, vi is controlled using certain command letters. In order to distinguish these from normal letters, you press the [Esc] key (short for escape) first.

Beeindruckendes GUI
Unfortunately the vi has not such a graphic user interface.

Invoking the Editor vi

The editor is launched by entering the command vi followed by the name of the file you want to edit as a parameter.

/a# vi test.txt

In this case the file test.txt does not yet exist, so vi will display nothing but empty lines, which are indicated by the tilde ~ characters at the lefthand border.

~
~
~
~
~
~
~
~
~
~
~
"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.

Saving Files

Though we haven't typed anything yet, let's first save our work, by typing the following keys after oneanother:

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

Exiting from the Editor vi

Exiting is similar to saving:

[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

Modes of Operation in vi

At any given moment in time vi can be in one of three operational modes. This is the main reason why some find this editor hard to use. Often it is not evident which the current mode is, which makes things worse.

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.

Entering Text

As mentioned above, vi is in command mode when first started. Attempting to enter text in this mode will create chaos, since all characters are interpreted as commands.

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.

Undoing the Last Command

Maybe one day you will enter a command you didn't really mean, so something might happen you didn't really want to happen. Don't panic. You can turn back time by typing one of the following undo commands:

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

Positioning the Cursor within the Text

Cursor keys are often not active in text mode, the corresponding control characters are entered into your poems instead! This can be the case when logged into a computer via the internet, online-fees ticking away in the back of your mind. So it's a good idea to practise navigation on dry land. In order to be able to reposition the cursor in your text, you need to switch to command mode again, using the [Esc] key.

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.

Leaving vi without saving

Since you don't want to corrupt this important readme-file, you will need to exit the editor without first saving. To do that you must enter an exclamation mark ! after the q.

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

Exercise

Create a text file /a/words_of_wisdom.txt. Type in the following variants first and save the file. Now edit the lines in such a way that the original words are recovered.

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
~
~
~
~
~
~
~

Search and Replace

vis capabilities for searching for text are more sophisticated than those of many other editors. For instance with vi you can search for words that begin with one of the letters b through f and end in ast.

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]

Commands for Deleting Text

In command mode you can delete text using the command [Esc] d. Alas, it is neccessary to combine this command with cursor positioning commands (s.a.), or even the search commands / and ?. When you've mastered cursor positioning (and maybe even searching), you've mastered deleting. Some examples:

[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

Copying and Moving Blocks of Text

The last text deleted is stored in a buffer. It can be inserted into the file using one of the following commands.

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

Editing More Than One File

Using vi you can edit more than one file at the same time. Should you want to edit all muLinux help files, to be found in the directory /usr/doc/help, you could invoke vi using the asterisk wildcard * thus:

/# 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]

Exercise: Editing Configuration Files

Most configuration files are to be found in the directory /etc. Let us edit some of these files.

/# cd /etc
/etc# _

The File /etc/issue

First of all, we shall change the file issue, which contains the systems login prompt. Before we destroy what we have, create a backup.

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

The File /etc/ls.conf

This file contains default settings for the ls command. Edit the file in such a way as to make -l the standard option for the command ls. That means, whenever you type ls, you automatically get the result of ls -l.

/# 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.


To end this chapter and call it a day, remove the mounting of the floppy disk.
/a# cd /
/# umount /a
/# _

[Back] [Home] [Forward]      
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
 
   
 
     

Zurück zur Themenseite:
StudyPaper.com/Startseite/Computer/Informatik

Das Setzen von Verweisen (Links) auf diese Seite ist gestattet und bedarf keine vorherige Absprache.
   
  Startseite  |  english  |  Bookmark setzen  |  Webseite weiterempfehlen  |  Copyright ©  |  Impressum