Presentation is loading. Please wait.

Presentation is loading. Please wait.

THE vi EDITOR. Introduction There are three editors available in almost all versions of Unix: ed, ex and vi. The ed program is the original editor that.

Similar presentations


Presentation on theme: "THE vi EDITOR. Introduction There are three editors available in almost all versions of Unix: ed, ex and vi. The ed program is the original editor that."— Presentation transcript:

1 THE vi EDITOR

2 Introduction There are three editors available in almost all versions of Unix: ed, ex and vi. The ed program is the original editor that has been a part of Unix since the very beginning. If you use it, you begin to appreciate how far s/w design has progressed since 1975. The ed program is basically a line editor, which means that ed assigns line numbers to the lines in the file; every time you do something, you must tell ed which line or lines to do it to. An improved version of ed, called ex, understands all the commands of ed. Compared to ed it is a shade better in user-friendliness with more informative error messages. The vi is a screen editor rather than a line editor; it shows you as much of the file as it can fit on the screen. It is the first full screen editor. It allowed the user to view and edit the entire document at the same time.

3 The vi Editor The vi was written by Bill Joy when he was a student at University of California. There are three modes in which the editor works. Under each mode the same keypress creates different effects. Hence meaning of several keys and their effects in each mode have to be memorised. Vi is fanatically case-sensitive. Vi can handle files that contain text. No fancy formatting, no fonts, no embedded graphics or junk like that, just plain simple text. You can create files, edit them and print them.

4 Modes of Operation The vi program has three modes of operation: (a)Command Mode : In this mode all the keys pressed by the user are interpreted to be editor commands. In command mode the keys that are hit are not displayed on the screen. (b) Insert Mode : This mode permits insertion of new text, editing of existing text or replacement of existing text. Each of these operations can be performed only after changing over from the command mode to insertion mode using appropriate commands. The insertion mode is also known as input-text mode. ( c) The ex Command Mode : This mode permits us to give commands at the command line. The bottom line of the vi screen is called the command line. Vi uses the command line to display messages and commands. This mode is so called because commands given in this mode are compatible with the commands of the ex editor.

5 Input Mode – Entering and Replacing Text Insert and append (i,a, I and A) Replace (r, R,s and S) Open a line (o and O) Always keep in mind that after you have completed text entry using any of these commands (except r), you must return to the Command Mode by pressing [Esc].

6 Inserting and Appending text (i and a) Inserting and Appending text at extremes (I and A) I inserts text at beginning of line. A appends text at end of line. EX: A comment line in C is of the form /* comment */. Use I on an existing line that you wish to convert to a comment, and then enter the symbols /*. After pressing [Esc], use A to append */ at the end of each line and press [Esc] again. Ex: to draw 70 asterisks, you should use a repeat factor. After you have entered / in input mode, press [Esc], and then enter 70a*[Esc]. You will see 70 asterisks appended to the /.

7 Opening a New Line (o and O): o – to open a line below from anywhere in a line O – to open a line above the current line. Replacing Text(r,s R and S) r: to replace a single character with another, (no [esc] required) followed by the character that replaces the one under the cursor. s: to replace one character with ‘n’ characters. To replace multiple characters, use a repeat factor. 3s replaces three characters with new text. R: replaces all text on the right of the cursor position. S: replaces the entire line irrespective of the cursor position.

8 Entering Control Characters ([Ctrl-v] ) If you write shell scripts to send some escape sequences to your printer or terminal, then you would need to enter control characters. Ex: to enter [ctrl-h], you have to first press [ctrl-v] and then [ctrl-h], you’ll then see on the screen as: ^H just one character here You can only position the cursor on the ^ and not on the H

9 Saving text and Quitting – the ex mode When you edit a file using vi, the original file isn’t disturbed, but only a copy of it is placed in a buffer, ( a temporary form of storage). These are the 3 operations that we commonly perform with the buffer: Save and continue editing (:w). Save and exit (:x and :wq). Abandon all changes and quit (:q and :q!)

10 Writing Selected Lines the w command can be prefixed by one or two addresses separated by a comma. Ex: :5w n2words.pl writes 5 th line to another file :10,50w n2words.pl writes 41 lines to another file :.w tempfile saves the current line :$w tempfile saves the last line :.,$w tempfile saves current line through end If tempfile exists and writable by you, vi issues warning: “tempfile” file exists – use “w! tempfile” to overwrite. The ! is the universal overriding operator in the ex Mode.

11 Navigation and Scrolling Commands Relative Motion: Command Function h (or [Backspace])Moves cursor left l (or [Spacebar])Moves cursor right 5lMoves 5 characters right k Moves cursor up 10kMoves 10 lines up j Moves cursor down

12 Scrolling: Command Function [Ctrl-f]Scrolls full page forward 5[Ctrl-f]Scrolls 5 full pages forward [Ctrl-b]Scrolls full page back [Ctrl-d]Scrolls half page forward [Ctrl-u]Scrolls half page back [Ctrl-l]Redraws the screen (no repeat factor)

13 Word Navigation: Command Function b Moves back to beginning to word 4bMoves back 4 words to beginning of word e Moves forward to end of word w Moves forward to beginning of word 8w Moves forward to beginning of 8 th word

14 Line Navigation and Absolute Movement: Command Function 0 (zero) or |Moves to beginning of line 30|Moves to column 30 ^Moves to first word in line $Moves to end of line 1GMoves to beginning of file 40GMoves to line 40 GMoves to end of file

15 Editing Text Deleting Text (x, X and dd): The x and X commands are used to delete one or more contiguous characters. x – character under the cursor gets deleted 4x – deletes the current character as well as 3 characters from the right X – handles the text deletion to the left. dd – to delete the current line in which the cursor is positioned. Moving Text (p): Put the deleted text at the new location with p or P. vi uses these two commands for all “put” operations that follow delete or copy operations. The significance of p and P depends on whether they are used on parts of lines or complete lines. If we work on parts of lines, P places text on the left of the cursor and p places on the right.

16 Joining Lines (J) : to join the current line and the line following it, use J. J removes the newline character between the two lines to pull up the line below it. Changing Case(~) : Vi uses the ~ (tilde) command to toggle the case of text. To reverse the case of a section of text, move the cursor to the first character of the section and then press - upper becomes lower, lower becomes upper. If you have to do this for a string of a hundred characters, use a repeat factor : 100~

17 Deleting and Moving text (d,p and P) : d$ - to delete text from the present cursor position to the end of line. dw – deletes one word 3dw – deletes three words dG – deletes from current cursor position to end of file dd – deletes a single line 6dd- deletes current line and five lines below Moving text When we delete entire lines, p places text below the current line and P place text above the current line. When we delete a partial line, we can put the deleted word only to the left(with p) or right (with P) of the cursor position.

18 Yanking Text (y,p and P) : The y operator yanks ( or copies ) text. You can yank a word, a group of words, line segments or even entire lines with this operator. Moreover, the p and P commands act in the same way for putting the copied text at its destination. Ex: to yank 5 lines of text, move the cursor to the first of these lines and press 5yy. Next, move the cursor to the new location, and press p or P. y$ - yanks text from current position to end of line y1G (or 1yG) – copies text from current cursor position to the beginning of the file.

19 Undoing Last Editing Instructions (u and U) The u command permits only single-level undoing. Another u will undo this too. When a number of editing changes have been made to a single line, vi allows you to discard all the changes before you move away from the line. The command U reverses all changes made to the current line.

20 Searching for a Pattern (/ and ?) Searching is initiated from the Command Mode by pressing a /, which followed by a string, then press [enter]. For ex: to look for string ‘printf’, enter /printf [Enter] The search begins forward to position the cursor on the first instance of the word. Vi searches the entire file, so if the pattern can’t be located till the end of file is reached, the search wraps around to resume from the beginning of the file. If the search still fails, vi responds with the message ‘Pattern not found’. Like wise, the sequence ?pattern[Enter] Searches backward for the most previous instance of the pattern. The wrap around feature also applies here but in the reverse manner.

21 Repeating the Last Pattern Search (n and N) : For repeating a search in the direction the previous search was made with / or ?, use n The cursor will be positioned at the beginning of the pattern. You can then carry out some editing function, say change a word with cw. You can press n repeatedly to scan all instances of the string and then repeat the change wherever you want. N reverses the direction pursued by n.

22 Repeating the Last Command (. ) The. (dot) key is used for repeating both Input and Command Mode commands. The principle is this : Use the actual command only once, and then repeat it at other places with the dot command. Ex: consider that you have to indent a group of lines by inserting a tab at the beginning of each line. You need to use i[Tab][Esc] only once, say on the first line. You can then move to each line in turn by hitting [Enter], and simply pressing the dot. A group of lines can be indented in no time.

23 Substitution- search and Replace ( :s) The ex Mode’s s(substitute) command lets you replace a pattern in the file with something else. The / serves as the delimiter of the syntax components: :address / source_pattern / target_pattern / flags The source_pattern is replaced by the target_pattern in all lines specified by address. The address can be one or a pair of numbers, separated by a comma. Ex: :1,$/double/float/g Can also use % instead of 1,$ Here double is replaced with float globally throughout the file.

24 : 1,50s/unsigned/ /g Deletes unsigned everywhere in lines 1 to 50 :3,10s/msg/message/g all occurrences in lines 3 through 10 :$s/msg/message/gall occurrences in last line :.s/echo/printfonly first occurrence in current line Interactive substitution: To selectively replace a string, add the c(confirmatory) parameter as the flag at the end: :1,$s/message/msg/gc


Download ppt "THE vi EDITOR. Introduction There are three editors available in almost all versions of Unix: ed, ex and vi. The ed program is the original editor that."

Similar presentations


Ads by Google