Transcript Slide 1
Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • vi Basics • operates in 3 modes • Command Mode • In this mode we pass commands to act on text • can not use this mode the enter or replace text • press the i key •Insert Mode • ready to input text • at each line <Enter> • backspace to wipe out unwanted text • [Ctrl-w] to erase the entire word Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Insert Mode • press [Esc] to revert back to Command Mode • press l (el) or h to move the cursor • h – left • l (el) – right • The entered text is not saved in the insert mode • to save the text from buffer switch to ex-Mode • ex-Mode or Last Line Mode • while in Insert Mode, enter : (colon), then enter x, press <Enter> • :x <Enter> Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • ex-Mode or Last Line Mode • The file is now saved and you are back to the $ prompt • Command Mode • The default mode where every key pressed is interpreted as a command to run on text • Navigation, copying, deleting text are performed in this mode • use h or l (el) to move the cursor along a line • use j and k to move the cursor up and down the lines Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Insert Mode • Invoke this mode by pressing on of the following keys • i – insert text to the left of the cursor • a – appends text to the right of the cursor • I – insert text at the beginning of the line • A – appends text at the end of the line • o – opens line below • O – opens line above • The File .exrc • vi reads the file $HOME/.exrc (same as ~/.exrc in some shells) on startup • You can create abbreviations, redefine your keys to behave differently Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Input Mode – Entering and Replacing Text • Insert and append (i, a, I, A) • Replace (r, R, s, S) • Open a line (o, O) • :set showmode • messages like INSERT MODE, REPLACE MODE, CHANGE MODE will now appear the last line • can add this in the .exrc file for permanent settings Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Saving Text and Quitting • :w – save and continue • :w anotherfile • It is common practice to ignore the readonly label on the file • what happens when trying :w? • :x and :wq – save and exit • :q and :q! – abandon all changes and quit • ZZ • it is dangerous • you might hit [Ctrl-z] and suspend the process Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Escape to the Shell (:sh and [Ctrl-z] • :sh • returns to shell prompt • execute cc to compile C program • use [Ctrl-d] or exit to return to the editor • use [Ctrl-z] to suspend current vi • use fg to return to editor Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Deleting Text • x – deletes one or more contiguous character • X – deletes to the left • dd – deletes the current line • Joining Lines • J – joins two lines • the current line and the line following it • Changing Case (~) • changes the case of the text Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition UNIX – The vi Editor • Copying lines • yy – copy current line • y3y – will copy 3 lines • “ayy and “ap to copy lines from one file to another • open the new file in ex Mode :e newfile • Paste • after yy; p will paste the lines • Undoing Last Editing • u – will undo the last change • Substitution – Search and Replace (:s) • 1,$s/double/float/g • 1,50s/unsigned//g Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Your UNIX: The Ultimate Guide second edition QUIZ Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved.