introduction to linux instructor: bennett m. tanyag part - 2

50
Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Upload: matthew-lucas

Post on 16-Jan-2016

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Introduction to Linux

Instructor: Bennett M. Tanyag

PART - 2

Page 2: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

History of vi

vi (pronounced “vee eye”) is really the only editor you can find at almost Unix installation.

vi stands for visual editor It was originally written at the University of

California at Berkeley Versions of vi can found in almost every

vendor’s edition of Unix It is initially somewhat hard to get used to, but it

has many powerful features.

Page 3: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

History of vi

Early text editors were line oriented and were typically used from dumb printing terminals.

A typical editor that operated in this mode is ed The editor was powerful and efficient, using a very small amount of

computer resources, and worked well the display equipment of that time

vi offers the user a visual alternative with a significantly expanded command set compared with ed.

Vi as we know it today started as the line editor, ex. In fact ex is seen as a special editing mode of vi, although actually

the converse is true The visual component of ex can be initiated from the command line

by using the vi command, or from within ex.

Page 4: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

William Joy developed the ex/vi editor at the University of California at Berkeley.

It was originally supplied as an unsupported utility until its official inclusion in the release of AT&T System 5 Unix

It has steadily become more popular, in spite of the challenges posed by more modern full screen editors.

Page 5: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

vi editor

The vi (visual) text editor of UNIX is for creating and editing text files such as letters, memos, reports, correspondences, and the like. vi is a screen-oriented text editor.

Page 6: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

There are two modes of operations in vi.

1.The text-edit mode is where the user can enter or edit text. When in the text-edit mode, each the user types a letter, that letter will enter the document the user is editing.

Page 7: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

2. The command mode is where the user issues commands to the editor. In this mode, a letter corresponds to a command and not a character to be entered in the text file.

For example, in the command mode, the letter h is for moving the cursor one position to the left. When the user types the letter h while in the text-edit mode, it enters the file. Therefore, the user has to be in the command mode in order to perform functions such as moving the cursor (moving around the document), deleting characters, words, or lines, and saving the document.

Page 8: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

The format of vi:vi filename(s)

Example:$ vi sample.text

Note: If you don’t specify any filename for vi, the filename must be specified later.

Page 9: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Sample vi interface

The tilde “~” character indicates unused lines. The last line on the screen is used by the editor for communicating information to the user.

Page 10: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

If the file exists, the content of the file will be shown up.

Page 11: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Upon entering the vi editor, the user is automatically taken into the command mode. At this point, the keys on the keyboard represent commands, that is, any character the user types will not enter the file as text but will cause the execution of a certain action.

Page 12: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

The cursor in vi is represented by an underscore (underline). In some UNIX versions, the cursor is seen as a box. Once UNIX starts the vi text editor, it places the cursor at the start of the first character of the document.

Page 13: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

As the user starts editing the file, the changes are not actually made on the file itself. Everytime vi starts, it copies the file from the disk and into main memory (edit buffer). All editing changes will be made in the edit buffer. The file on the disk remains intact. Changes on the actual document will be made once the user saves the document. In this way, the user may choose to abandon the changes and leave the document as it was.

Page 14: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

MOVING IN vi

In the command mode, the user may move the cursor anywhere in the document by pressing any of the following keys (no need to press the Enter key):

Page 15: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

h or <BACKSPACE> move 1 position to the left

l or <SPACEBAR> move 1 position to the right

j move one line down

k move one line up

w move one word to the right

b move one word to the left

^ move at the beginning of a line

$ move at the end of the line

# Shift + g move to line #

Ctrl-F move forward 1 full page

Ctrl-B move backward 1 full page

Shift + g move to last line of the document

MOVING IN vi

Page 16: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Ctrl-D move forward 1/2 page

Ctrl-U move backward 1/2 page

- move to the first character of the preceding line

Shift + h move to top of the screen

Shift + m move to the middle of screen

Shift + l move to bottom of the screen

MOVING IN vi

Page 17: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

If the user types a number before certain commands, it will cause the cursor to move several units. Examples are:1. Pressing 5h will cause the cursor to move 5 options to the left.2. Pressing 10k will cause the cursor to move 10 lines up.3. Pressing 3w will cause the cursor to move 3 words to the right.

Page 18: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

DELETING TEXT IN vi

While in the command mode, the user may also delete characters, words, or lines. Just move the cursor to the character, word, or line to be deleted and then press the corresponding keys:

Page 19: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Command Action x delete one character dw delete a word dd delete a line

D delete from the current cursor position to the end of line

DELETING TEXT IN vi

Page 20: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

If the user types a number before any of these commands, it will cause the deletion of several units. Examples are:

1. Pressing 12x will delete the next 12 characters.

2. Pressing 7dw will delete the next 7 words.

3. Pressing 6dd will delete the next 6 lines.

DELETING TEXT IN vi

Page 21: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

EDITING TEXT IN vi Command Action

i insert new text before the cursor position

Shift + i insert new text at start of the current line

a append new text after the cursor position

Shift + a append new text at end of current line

o open a new line after the current line

Shift + o open a new line before the cursor position

r change a character (no need to press <ESCAPE> key afterwards

Page 22: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

EDITING TEXT IN viShift + r overwrite everything until the <ESCAPE>

key is pressed

cw change word

Shift + c change all text from current cursor position

~ convert from upper to lower case and vice versa

u undo the last editing change

shift + u undo changes in a line

. repeat the last editing change

shift + j join two lines

Note: Press the ESC key after making changes.

Page 23: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

EXITING vi

To exit the vi editor, just simple type a colon ‘:’ (the user should be in the command mode). A colon will appear at the bottom of the screen. At this point, the user can type any of the following (followed by the Enter key):

Page 24: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Command Action q quit vi q! quit vi without

saving changes wq save changes and

then quit vi x same as wq

Page 25: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

SAMPLE vi SESSION

The text file below contains several errors (line numbers were placed for easy reference):

Page 26: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

1 12 Octoer 200323 Mr. Linus Torvalds45 Dear Linus Torvalds67 Iam happy to report the successful completion of8 the UNIX Training Projevt910 Thank you very much.1112 Sincerely yours,1314 Bill Gates~~~“sample.text” 15 lines, 203 characters

Page 27: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Making the corrections:

1.To correct the spelling of “October” in line # 1:Just simply put the cursor at the second “o” of the word “October” and press a (append text after current cursor position). The user is now in the text-edit mode. Type b to correct the spelling. Press the ESC key to go back to the command mode.Another option is to place the cursor at “e” and press i (insert new text before the current cursor position).

Page 28: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

2.To place the title “President” under “Linus Torvalds”The user should place the cursor at line #3 and press o (open new line after the current line). Since the user is now in the text-edit mode, he can then type the desired text. Then press the ESC key to go back to the command mode.Another option is to place the cursor at line # 4 and press O (open a new line before the current line). The type the required text.

Making the corrections:

Page 29: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

3.To insert a space in the world “Iam” in line # 7:

Place the cursor in “I” and press a. Then insert a space by pressing the Spacebar key.

Another option is to place the cursor at “a” and then press i followed by a space.

Making the corrections:

Page 30: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

4.To correct the spelling of “Projevt” in Line # 8:

Place the cursor at “v”. Press x to delete the unwanted character. At this point, the cursor is at “t”. Press i to insert the correct character “c”.

Making the corrections:

Page 31: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

NOTE: In the vi editor, there is no indication which mode of operation a user is currently in. If he is not sure if vi is in the text-edit or command mode, he should simply press the ESC key and from then on, he is sure that vi is in the command mode.

Page 32: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

OTHER FEATURES OF vi

Pattern SearchingA user may search for any word or string in the file while inside vi. The user should first be in the command mode. Typing a slash “/” will cause the cursor to go to the bottom of the screen. The user may then type in a word or phrase he wants to locate. Pressing the Enter key will cause vi to start searching. If the word or phrase exists, then the cursor will jump to its next occurrence. If the pattern does not exist, then vi will inform the user by showing a message at the bottom of the screen.

Page 33: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Search and Replace

To search for a particular word, phrase, or pattern and replace it with another one, type a colon “:” and the cursor will move to the bottom of the screen. Then type %s/old_pattern/new_pattern where old_pattern is the pattern to be replaced while new_pattern is the pattern that will replace old_pattern.

OTHER FEATURES OF vi

Page 34: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Block CopyTo copy a block of text in vi, first place the cursor at the start of the block (the user should be in the command mode). Then type #yy where # is the number of lines of the block to be copied. Then move the cursor to the position where the block is to be copied and then press p.

OTHER FEATURES OF vi

Page 35: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Block Move

The procedure for moving blocks of text in vi is the same as with the block copy except that the user should type the command #dd instead of #yy.

OTHER FEATURES OF vi

Page 36: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

C programming in Linux Linux has a C compiler bundled in the OS.

The process to creating a C program is very similar to creating an ordinary C program in other compilers. However, the program should be encoded under vi, then execute the command cc in order to compile the program.

Page 37: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Syntax for cc

cc <filename>.c

Where:cc command name<filename>.c is the source code of the

program.

C programming in Linux

Page 38: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Sample program:#include<stdio.h>void main(){

printf(“\nC in Linux\n”);}

Note: programs should be encoded using the vi editor.

C programming in Linux

Page 39: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

After you’ve finished encoding the program, you must compile it. The next step would be:

$ cc first.c

Performing this command will produce a file named a.out.

C programming in Linux

Page 40: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

The a.out file is the compiled version of your program. To execute the file type this command:

$./a.outC in Linux$_

C programming in Linux

Page 41: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Some considerations: If there are errors encountered in your

program, a.out will not be produced. If a.out exist and errors are encountered a.out

will not be updated. If you compiled another program, a.out will be

overridden. (assuming there are no errors)

C programming in Linux

Page 42: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Since, a.out is the executable version of your program. It would be advisable to rename a.out to a more descriptive name. For instance, if your program name is compute.c then it would be appropriate to rename a.out to compute.exe (though extension file name does not apply to Linux).

C programming in Linux

Page 43: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

Try this

Create a C program that will ask an integer input from the user. Your program must determine if the number is odd number or an even number.

Note: name your file as prog1.c

Page 44: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

More on cc Command

It would be very tiring to rename a.out every time we compile the program. Recall that we need to rename a.out if we don’t want it to be replaced by other programs when compiling a different source code.

$cc prog1.c $mv prog1.c prog1.exe

Page 45: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

More on cc Command

Another way we can do is using to –o option of cc. The –o option allows you to give an output file name. In this case, instead of a.out to be produced by compiler the filename you specified will be generated.

Syntax:cc prog1.c –o prog1.exe

Page 46: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

PRINTING TEXT FILES

The lp command in UNIX is the command for printing files.

Example:

$ lp thesis.doc

This will print the file thesis.doc

Page 47: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

lp command

The lp command does not actually print data; instead, it makes note of the file to be printed. After typing the command, the $ prompt returns immediately, even though nothing has yet printed. In other words, the lp command allows the user to request the printing of a file.

Page 48: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

lpsched

The lpsched program is the one responsible for printing the files. This program runs in the background and does not interact with the user. The lpsched program is commonly known as the print daemon.

Page 49: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

lpsched

lpsched will handle multiple print requests on a first come, first served basis. Print jobs will have to wait in a print queue for its turn to be printed.

Page 50: Introduction to Linux Instructor: Bennett M. Tanyag PART - 2

END OF PART 2 – UNIT 1