chapter 3 mastering editors guide to unix using linux fourth edition chapter 3 (40 slides)1 ctec 110
TRANSCRIPT
Chapter 3
Mastering Editors
Guide To UNIX Using Linux Fourth Edition
Chapter 3 (40 slides) 1CTEC 110
• Explain the basics of UNIX/Linux files, including ASCII, binary, and executable files
• Understand the types of editors• Create and edit files using the vi editor• Create and edit files using the Emacs
editor
Objectives
Chapter 3 (40 slides) 2CTEC 110
• Almost everything you create in UNIX/Linux is stored in a file
• Bit: binary digit– In one of two states: 0 or 1
• Machine language: exclusive use of 0s and 1s as a way to communicate with computer– Used by earliest programmers
Understanding UNIX/Linux Files
Chapter 3 (40 slides) 3CTEC 110
• Byte (binary term): string of eight bits• A byte can be configured into fixed patterns of bits
– ASCII: American Standard Code for Information Interchange▫ 256 different characters
– Unicode▫ Supports up to 65,536 characters
• Text files: contain nothing but printable characters• Binary files: contain nonprintable characters
– Example: machine instructions
ASCII Text Files
Chapter 3 (40 slides) 4CTEC 110
• Some things cannot be represented with ASCII codes
• Binary files are used instead– Example: graphic files include bit
patterns▫ Bitmap: made of rows and columns of dots
Binary Files
Chapter 3 (40 slides) 6CTEC 110
• Text files containing program code are compiled into machine-readable language
• Scripts are files containing commands– Typically interpreted, not compiled
• Executables: compiled and interpreted files that can be run
Executable Program Files
Chapter 3 (40 slides) 7CTEC 110
• Editor: program for creating and modifying files containing source code, text, data, memos, etc.
• Text editor: a simplified word-processing program– Used to create and edit documents
• Two text editors normally included in UNIX/Linux are screen editors– vi– Emacs
• Line editor: works with one line (or group of lines) at a time
Using Editors
Chapter 3 (40 slides) 8CTEC 110
• vi is a visual editor• vi is also a modal editor
– Supports three modes▫ Insert mode
› Accessed by typing “i”
▫ Command mode› Accessed by typing Esc
▫ Extended (ex) command set mode› Accessed by typing “:” in command mode
• Download the vi editor document on Canvas for reference
Using the vi Editor
Chapter 3 (40 slides) 9CTEC 110
• When you start vi, you are in command mode
• To insert text in your file, switch to insert mode– Use i (insert) command
• To return to command mode, press Esc
Inserting Text
Chapter 3 (40 slides) 11CTEC 110
• Use a period (.) to repeat the most recent change you made– Repeat command– Works in command mode
Repeating a Change
Chapter 3 (40 slides) 12CTEC 110
• To move cursor use arrow keys (command/insert mode) or (in command mode) use:
Moving the Cursor
Chapter 3 (40 slides) 13CTEC 110
• Deletion commands available (command mode)
• dd is used for “cutting” text– Use “yank” (yy) command for “copying” text
Deleting Text
Chapter 3 (40 slides) 14CTEC 110
• Type u to use the undo command• Example:
– If you delete a few lines from a file by mistake, type u to restore the text
Undoing a Command
Chapter 3 (40 slides) 15CTEC 110
• To search forward for a pattern of characters:– Type a forward slash (/)– Type the pattern you are seeking– Press Enter
• Examples: /\<top, /s..n, /pas[st], /!$
Searching for a Pattern
Chapter 3 (40 slides) 16CTEC 110
• Screen-oriented commands execute at the location of the cursor
• Line-oriented commands require you to specify an exact location (an address) for the operation– Preceded by a colon (:)– Operate in ex mode– Used for commands that perform more than one
action▫ Example: searching and replacing
:1,$s/insure/ensure/g
Searching and Replacing
Chapter 3 (40 slides) 17CTEC 110
• To save file without exiting, use :w• To save and exit, use :wq, :x, ZZ
(Do this in command mode)
Saving a File and Exiting vi
Chapter 3 (40 slides) 18CTEC 110
• To copy entire contents of one file into another file:– Use vi to edit the file you would like to
copy into– Use the command :r filename
▫ filename is the name of the file that contains the information you want to copy
Adding Text from Another File
Chapter 3 (40 slides) 19CTEC 110
• To launch a shell or execute other commands from within vi, use :!– Example:
▫ :!cal
• To run several command-line commands in a different shell without closing vi session– Use Ctrl+z to display the command line– Type fg to go back to vi
Leaving vi Temporarily
Chapter 3 (40 slides) 20CTEC 110
• To turn on line numbering:set number
• To delete lines 4 through 6 (ex mode):4,6d
Changing Your Display While Editing
Chapter 3 (40 slides) 22CTEC 110
• The command yy copies (yanks) a specified number of lines– To cut the lines, use dd– Lines are placed in clipboard
• Use p to paste the clipboard contents
Copying or Cutting and Pasting
Chapter 3 (40 slides) 23CTEC 110
• To print a file, use the lpr (line print) shell command– Example:
:!lpr -P lp2 accounts
Printing Text Files
Chapter 3 (40 slides) 24CTEC 110
• Canceling an editing session will discard all the changes you have made
• Or, save changes you made since last using :w– Saves file without exiting vi
Canceling an Editing Session
Chapter 3 (40 slides) 25CTEC 110
• Use the help command– :help
• Other alternatives:– man vi
▫ From the command line
– :!man vi▫ From vi (command mode)
Getting Help in vi
Chapter 3 (40 slides) 26CTEC 110
• Emacs is a popular UNIX/Linux text editor– Not modal– More complex than vi– More consistent than vi– Sophisticated macro language
▫ Macro: set of commands that automates a complex task
▫ Uses: read mail, edit contents of directories, etc.
– Powerful command syntax– Extensible
Using the Emacs Editor
Chapter 3 (40 slides) 27CTEC 110
• To create a new file: emacs filename• To navigate in the file, use the cursor
movement keys or Ctrl/Alt key combinations– Example: Alt+f
• To save your work:– Use File menu– Use the save icon– Press Ctrl+x, Ctrl+s
• To exit: use menu, icon, or Ctrl+x, Ctrl+c
Navigating in Emacs
Chapter 3 (40 slides) 31CTEC 110
• Del or Backspace keys delete individual characters
• Ctrl+k deletes to the end of a line• To undo a deletion, use Ctrl+x, u
– Repeatedly undoes each deletion
Deleting Information
Chapter 3 (40 slides) 32CTEC 110
• To Copy-Paste or Cut-Paste:– Mark the text
▫ Position cursor at the beginning, and Ctrl+Spacebar
▫ Navigate to the end of the text you want to include:› Alt+w copies the text› Ctrl+w cuts the text
– To paste, move to where you want to place the text ▫ Ctrl+y (the yank command)
Copying, Cutting, and Pasting Text
Chapter 3 (40 slides) 33CTEC 110
• One way to search in Emacs is to:– Press Ctrl+s– Entering string to find (on status line)– Pressing Ctrl+s repeatedly to find each
occurrence
• Use Ctrl+r to search backward• Other alternatives:
– Use search forward for a string icon– On the menu: Edit Search Search
Searching in Emacs
Chapter 3 (40 slides) 34CTEC 110
• Alt+q turns on word wrap feature– Lines automatically wrap around from
one line to the next
Reformatting a File
Chapter 3 (40 slides) 35CTEC 110
• Emacs comes with extensive documentation and a tutorial– Tutorial is useful for getting up to speed
quickly▫ Click Help menu Emacs Tutorial▫ Or (in most versions), type Ctrl+h and then
type t
– To view general Emacs documentation:▫ Ctrl+h (press one or two times)▫ Or, man emacs at command line
Getting Help in Emacs
Chapter 3 (40 slides) 36CTEC 110
• Bytes: computer characters (a series of bits) stored using numeric codes
• The vi editor is popular among UNIX/Linux users– Three modes: insert (i), command (Esc), and ex (Esc :)– With vi, you edit a copy of the file placed in memory
▫ File is not altered until you save it on disk
• Emacs is a popular alternative to vi– Supports powerful command syntax and is extensible– Insert text simply by typing– Sophisticated macro language
Summary
Chapter 3 (40 slides) 37CTEC 110