Download presentation
Presentation is loading. Please wait.
Published byMatthew Lucas Modified over 9 years ago
1
Introduction to Linux Instructor: Bennett M. Tanyag PART - 2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
If the file exists, the content of the file will be shown up.
11
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.
12
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.
13
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.
14
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):
15
h or move 1 position to the left l or move 1 position to the right jmove one line down kmove one line up wmove one word to the right bmove one word to the left ^move at the beginning of a line $move at the end of the line # Shift + gmove to line # Ctrl-Fmove forward 1 full page Ctrl-Bmove backward 1 full page Shift + gmove to last line of the document MOVING IN vi
16
Ctrl-Dmove forward 1/2 page Ctrl-Umove backward 1/2 page -move to the first character of the preceding line Shift + hmove to top of the screen Shift + mmove to the middle of screen Shift + lmove to bottom of the screen MOVING IN vi
17
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.
18
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:
19
CommandAction xdelete one character dwdelete a word dddelete a line Ddelete from the current cursor position to the end of line DELETING TEXT IN vi
20
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
21
EDITING TEXT IN vi Command Action iinsert new text before the cursor position Shift + iinsert new text at start of the current line aappend new text after the cursor position Shift + aappend new text at end of current line oopen a new line after the current line Shift + oopen a new line before the cursor position rchange a character (no need to press key afterwards
22
EDITING TEXT IN vi Shift + roverwrite everything until the key is pressed cwchange word Shift + cchange all text from current cursor position ~convert from upper to lower case and vice versa uundo the last editing change shift + uundo changes in a line.repeat the last editing change shift + jjoin two lines Note: Press the ESC key after making changes.
23
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):
24
Command Action q quit vi q!quit vi without saving changes wqsave changes and then quit vi xsame as wq
25
SAMPLE vi SESSION The text file below contains several errors (line numbers were placed for easy reference):
26
1 12 Octoer 2003 2 3 Mr. Linus Torvalds 4 5 Dear Linus Torvalds 6 7 Iam happy to report the successful completion of 8 the UNIX Training Projevt 9 10 Thank you very much. 11 12 Sincerely yours, 13 14 Bill Gates ~ ~ “sample.text” 15 lines, 203 characters
27
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).
28
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:
29
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:
30
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:
31
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.
32
OTHER FEATURES OF vi Pattern Searching A 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.
33
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
34
Block Copy To 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
35
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
36
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.
37
Syntax for cc cc.c Where: cc command name.c is the source code of the program. C programming in Linux
38
Sample program: #include void main(){ printf(“\nC in Linux\n”); } Note: programs should be encoded using the vi editor. C programming in Linux
39
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
40
The a.out file is the compiled version of your program. To execute the file type this command: $./a.out C in Linux $_ C programming in Linux
41
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
42
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
43
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
44
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
45
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
46
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
47
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.
48
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.
49
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.
50
END OF PART 2 – UNIT 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.