Presentation on theme: "Chapter 5 Editing Text Files"— Presentation transcript:
1 Chapter 5 Editing Text Files Mr. Mohammad Smirat
2 IntroductionThe editors that we consider here are all full screen display.The one we are going to explain here is the VI editor.The other editors are PICO and EMACS.
3 The VI editorThe UNIX VI editor has almost all the features of a word processor and tremendous flexibility in creating text files.VI is more complex than pico editor, but it gives the ability to work on much larger files.The vi editor is a standard part of UNIX, others like emacs you have to buy it separately.
4 The VI editorThe VI editor can be invoked from the shell with an optional filenames list as argument. $vi filename $vi file1 file2 // vi will edit them one by one $vi // you have to name a file // before you write the text bufferIt will start with the first line of the file as current line.$vi +30 filename will start the editor with the line 30 in the file filename is the current line.
5 The VI editor$vi +/string filename will make the line of the first occurrence of the string in the file filename is the current line.The VI editor fills the screen with the text file, leaving only one line at the bottom for special info, if the file less than a page it fills the screen with empty line begin with tilde ~.
6 BuffersThe buffer is a temporary storage facility for the text that you are editing.It is very useful and important in VI (for Visual).The main buffer, sometime referred to as editing buffer, or the work buffer, is the main repository for the body of the text that you are trying to create or modify from some previous permanently archived file on disc.The general purpose buffer is where your most recent “cut copied text” is retained.
7 The VI three ModesCommand mode, when vi it starts, it is in command mode, lot of commands you can use here such as when pressing i for input it will move you to input mode just before the current cursor location, with upper case I vi will enter input mode at the beginning of the current line.Input mode enter the text you want, every thing you enter goes to the file, backspace key lets you back up, deleting chars as you go, until the end of the line.Last line mode, you invoke this by entering : colon. The cursor will jump to the last line of the screen and vi will wait for your command :r file2 will read file2 into the current buffer after the current line.
9 Switching to Insert Mode a append text after the char the cursor is on.A append text after the last char of current line.c begin a change operationC change from cursor to end of line.i insert before the char where the cursor is on.I insert at the beginning of current line.o open a blank line below the current line.O open a blank line above the current line.R begin overwriting text.s substitutes single character.S substitutes whole lines.
10 Important Command For the Command Mode When you want to change from insert mode to command mode, press the ESC key.d deletes words, lines, etc.u undoes the last edit.P pastes -insert- the yanked or deleted lines before the current line.p paste the yanked or deleted lines after the current line.ZZ quit vi, saving the file only if changes were made since last save.
11 Examples of Command Syntax of VI editor 5dw delete five word, starting at the current cursor position. If the current line ends it will continue with the next line.Words in vi are seperated with spases, taps, “enter”, or any other special characters like comma, point, /, :, …etc.7dd deletes 7 lines, starting at the current line.c2b changes back two words starting from the preceding character for the current cursor position. If the current it reached the beginning of the line it will continue with the previous line.1G puts the cursor on the first line of the file.10yy Yanks (copies) the next 10 lines starting with current line into temporary buffer.
12 Cursor Movement and Editing Commands 1G moves the cursor to the first line.G move the cursor to the last line.0 (zero) move the cursor to the first char of the current line.ctl-g orG report the position of cursor in terms of line # and col #.$ moves the cursor to last char of the line.w moves the cursor forward one word.b moves the cursor backward one word.x delete the char at the cursor position.dd delete the line at the current cursor pos.u undoes the most recent change.r replace the char at the current cursor location with what is typed next.
13 Yank and Put (copy and paste) and Substitute (search and replace). In general you use the yank and put in sequence and move the cursor only between yanking and putting.Substitute command is executed from the last line mode where you preface the command with the : char and ends the command by pressing ENTER.
14 Yank and Put (Some Examples) y2w yanks two words, starting at the current cursor position, going right.4yb yanks 4 words, starting at the character previous to the current position going to the left.yy or y yanks the current line.P put the yanked text before the current cursor position.p put the yanked text after the current line5p put the yanked text in the buffer five times after the current cursor position.
15 The Last Line Mode :5,25w file3 write line 5 through 25 into file3. :w file4 write the current buffer into file4, if exists will ask you to confirm.:!ls temporary suspend vi, execute the shell command, when completed vi returned.:r filename reads and insert the contents of the file filename after the current line.:q! Quits vi without saving:wq save the buffer and quit.:w! filename overwrite file name with current text.:7,15d delete lines 7 through 15 in the buffer.
16 Substitute:[range]s/old-string/new-string[/option] where : is the colon prefix Range is a valid specification of lines S is the syntax for substitute Old-string text you want to replace New-string is the new text /option is a modifier usually g for global
17 Substitute (Some Examples) :s/john/jane/ substitute the word jane for the word john the current line only.:s/john/jane/g substitute the word jane for every word john on the current line.:1,10s/john/jane/g substitute the word jane for the word john on line 1-10.:1,$s/john/jane/g substitute the word jane for the word john for every word in the entire document.
18 Setting the VI Environment you can use any of several environment options to customize the behavior of the vi editor.These include specifying maximum line length and automatically wrapping the cursor to the next line.You can use the options using the :set command (in the last line mode).The following are some of the important environment option you can set.autoindent or ai align the new line with the beginning of the previous one.
19 Setting the VI Environment (cont…) ignorcase or ic ignore the case of a letter during the search process.number nu display line number when a file being edited.showmode (not shm) display the current vi mode in the bottom right corner of the screen.wrapmargin wm sets the wrap margin in terms of the number of chars from the end of the line.:set wm=6 showmode nu ic
20 Examples:set showmode vi will display an on-screen indicator when you are in input mode.:set all will display all options:set noshowmodeThe option you set with the :set are only active for the current editing session.