11
Using EditorsUsing Editors
Editors let you create and edit ASCII filesEditors let you create and edit ASCII files
UNIX normally includes two editors: vi and UNIX normally includes two editors: vi and EmacsEmacs
Vi and Emacs are screen editors: they Vi and Emacs are screen editors: they display the text you are creating (or display the text you are creating (or editing) one screen at a timeediting) one screen at a time
22
Using the vi EditorUsing the vi Editor
Called vi because it is visual – it immediately Called vi because it is visual – it immediately displays on screen the changes that you make displays on screen the changes that you make to textto text
works in different modesworks in different modes– Insert mode - lets you enter textInsert mode - lets you enter text– Command mode - lets you enter commands to Command mode - lets you enter commands to
perform editing tasksperform editing tasks
33
Using the vi EditorUsing the vi Editor
To create a new file in the vi editor, type vi and the name of the new file at the command prompt
44
Using the vi EditorUsing the vi Editor
When started, the vi editor is in command modeWhen started, the vi editor is in command mode– In order to insert text, you must issue the “i” command In order to insert text, you must issue the “i” command
to enter insert modeto enter insert mode– Esc key will change vi from text mode to command Esc key will change vi from text mode to command
modemode– You can repeat the line just entered with the repeat You can repeat the line just entered with the repeat
command (.)command (.)– To edit what you’ve just typed, move the cursor with To edit what you’ve just typed, move the cursor with
the various keyboard cursor movement keysthe various keyboard cursor movement keys
55
Using the vi EditorUsing the vi Editor
In insert mode, every character you type appears on the screen
66
Using the vi EditorUsing the vi Editor
The repeat command repeats the last insertion, or line. Now, edit the new line in order to create the next item
77
Using the vi EditorUsing the vi Editor
88
Vi-Correcting text keysVi-Correcting text keys
xx Deletes the character specified by the Deletes the character specified by the cursor positioncursor position
dddd Deletes the line specified by the cursor Deletes the line specified by the cursor positionposition
uu Undoes the most recent changeUndoes the most recent change
UU Undoes all the changes on the current lineUndoes all the changes on the current line
rr Replaces a character that the cursor is onReplaces a character that the cursor is on
RR Replaces characters starting from the Replaces characters starting from the cursor position, and changes vi to the text cursor position, and changes vi to the text modemode
99
Exit from vi editorExit from vi editor
Save - :w Save - :w
Save as different file - :w filenameSave as different file - :w filename
Save file and exit -- :wq filenameSave file and exit -- :wq filename
Exit without saving file - :q!Exit without saving file - :q!
1010
practicepractice
1111
Using the vi EditorUsing the vi Editor
1212
Using the vi EditorUsing the vi Editor
Use the delete commands and the cursor movement keys to edit the text
1313
Using the vi EditorUsing the vi Editor
1414
Using the vi EditorUsing the vi Editor
1515
Using the vi EditorUsing the vi Editor
The status line at the bottom of the screen displays information, including line-oriented commands and error messages
1616
Using the vi EditorUsing the vi Editor
During text searches, you can replace, too. Use line-oriented commands in ex mode to perform this action
1717
Using the vi EditorUsing the vi Editor
Saving a File and Exiting viSaving a File and Exiting vi– You should always save the file before exiting You should always save the file before exiting
vi, otherwise changes are lostvi, otherwise changes are lost– To save a file and continue working on it, type To save a file and continue working on it, type
the :w (write) commandthe :w (write) command– While in command mode, use the :wq (write While in command mode, use the :wq (write
and quit) command to save and exit iv, or and quit) command to save and exit iv, or the :zz command to exit after saving the :zz command to exit after saving
1818
Using the vi EditorUsing the vi Editor
In vi, you can also:In vi, you can also:– Add text from another fileAdd text from another file– Leave vi temporarily to perform other UNIX Leave vi temporarily to perform other UNIX
tasks, then return to your filetasks, then return to your file– Change your display while editing, such as Change your display while editing, such as
adding line numbering to help editingadding line numbering to help editing– Copy, cut, and paste text to help editingCopy, cut, and paste text to help editing– Print text filesPrint text files– Cancel an editing sessionCancel an editing session
1919
Using the vi EditorUsing the vi Editor
2020
Using the vi EditorUsing the vi Editor
2121